javascriptonsen-uijaydata

onsenui and jaydata doesn't coexist


when using this order :

<script src="lib/angular/angular.js"></script>
<script src="lib/onsen/js/onsenui.js"></script>
<script src="http://include.jaydata.org/datajs-1.0.3.js"></script>
<script src="http://include.jaydata.org/jaydata.js"></script>
<script src="http://include.jaydata.org/jaydatamodules/angular.js"></script>

I get this error

TypeError: Class.extend is not a function  
    at Object.<anonymous> (onsenui.js:13049)  
    at Object.invoke (angular.js:4535)  
    at Object.enforcedReturnValue [as $get] (angular.js:4387)  
    at Object.invoke (angular.js:4535)  
    at angular.js:4352  
    at getService (angular.js:4494)  
    at Object.invoke (angular.js:4526)  
    at Object.enforcedReturnValue [as $get] (angular.js:4387)  
    at Object.invoke (angular.js:4535)  
    at angular.js:4352

And when using this order:

<script src="lib/angular/angular.js"></script>
<script src="http://include.jaydata.org/datajs-1.0.3.js"></script>
<script src="http://include.jaydata.org/jaydata.js"></script>
<script src="http://include.jaydata.org/jaydatamodules/angular.js"></script>
<script src="lib/onsen/js/onsenui.js"></script>

note: edited to correct the order

jaydata.js:3342 Uncaught TypeError: Cannot read property 'apply' of undefined

any help is appreciated!


Solution

  • Finally I found a solution based on Ilia Yatchev answer so thanks to him.

    first I used the downloaded jaydata files instead of the online files since it contains the code change that Ilia Yatchev mentionned on his answer

    var Class;
    $data.Class = Class = new ClassEngineBase();
    

    note : the order isn't a problem now

    <script src="scripts/platformOverrides.js"></script>
    <script src="lib/angular/angular.js"></script>
    <script src="lib/onsen/js/onsenui.js"></script>
    <script src="lib/datajs-1.0.3.js"></script>
    <script src="lib/jaydata.js"></script>
    <script src="lib/jaydatamodules/angular.js"></script>
    

    then in the code we save a reference of the created entity:

    var Todo = $data.Entity.extend("Todo", {
        Id: { type: "int", key: true, computed: true },
        Task: { type: String, required: true, maxLength: 200 },
        DueDate: { type: Date },
        Completed: { type: Boolean }
    });
    

    and we save a reference of the created context:

    var TodoDatabase = $data.EntityContext.extend("TodoDatabase", {
        Todos: { type: $data.EntitySet, elementType: Todo }
    });
    

    then we can safely work these references:

    var todoDB = new TodoDatabase("MyTodoDatase");
    todoDB.onReady(function() {
        var tasks = todoDB.Todos.addMany([
            { Task: 'Step0: Get this this list', Completed: true },
            { Task: 'Step1: Define your data model'},
            { Task: 'Step2: Initialize data storage'}
        ]);
        todoDB.saveChanges(function() {
            tasks.forEach( function(todo) { alert(todo.Id) });
        });
    });
    

    if I don't save reference to the entity and the context I get this error

    angular.js:12722 ReferenceError: Contact is not defined
        at Object.<anonymous> (app.js:343)
        at Object.invoke (angular.js:4535)
        at Object.enforcedReturnValue [as $get] (angular.js:4387)
        at Object.invoke (angular.js:4535)
        at angular.js:4352
        at getService (angular.js:4494)
        at Object.invoke (angular.js:4526)
        at extend.instance (angular.js:9380)
        at nodeLinkFn (angular.js:8497)
        at compositeLinkFn (angular.js:7929)