onsenui 和 jaydata 不共存

onsenui and jaydata doesn't coexist

使用此订单时:

<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>

我收到这个错误

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

并且当使用这个顺序时:

<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>

注意:已编辑以更正顺序

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

感谢任何帮助!

jaydata 和 onsenui 似乎都在使用 window.Class,但它们的实现方式有很大不同。 Onsen 使用 John Resig 的实现,而在 jaydata 的版本中 Class 实际上是他们 ClassEngineBase 的一个实例。至少从我的角度来看,使这两个实现一起工作的问题在于,在 jaydata 的版本中 Class 实际上是一个实例而不是一个函数。如果它是一个函数,那么只需将扩展方法添加到 jaydata 的实现中就可以很容易地合并这两个实现。

您仍然可以尝试这样做,但不会那么容易,如果操作不当,可能会出现一些新错误。

所以你的选择是:

  1. 仍然尝试合并实现
  2. 修改温泉UI
  3. 修改 Jaydata
  4. 等待两个库之一解决问题

1。 序列:[jaydataonsenuipatch],其中patch是onsenui版本的修改版。

(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  this.Class.extend = function(prop) {
    var _super = this.prototype || {};
    initializing = true;
    var constructor = typeof this === 'function' ? this : function(){};
    var prototype = new constructor();
    initializing = false;

    for (var name in prop) {
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
            this._super = _super[name];
            var ret = fn.apply(this, arguments);
            this._super = tmp;
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }

    function Class() {
      if (!initializing && this.init)
        this.init.apply(this, arguments);
    }

    Class.prototype = prototype;
    Class.prototype.constructor = Class;
    Class.extend = arguments.callee;
    return Class;
  };
})();

不过我还没有真正测试过它是否有效,可能会有一些问题。

  1. 您没有包括 angular-onsenui.js,所以我猜您使用的是温泉 1,而不是温泉 2。Here 是 [=20= 的略微修改版本] 应该不会有碰撞(抱歉,没有测试)。

  2. 在 JayData 中,他们将 class 存储在 $data.Class 中,因此在 http://include.jaydata.org/jaydata.js 中只有 2 个地方使用了全局的

    2874: $data.Class = Class = new ClassEngineBase();
    3342: global["$C"] = function () { Class.define.apply(Class, arguments); };
    

您可以删除第一行的 = Class 并将第二行的两种情况从 Class 更改为 $data.Class,或者只在行上写 var Class; 2873.- 实际上他们似乎已经有了 implemented this change 但似乎还没有在线版本。

  1. 因此,如果您不想更改文件,我猜 JayData 可能在某处有更新版本。对于 Onsen - Onsen 1 的开发已经完成,我们只在 Onsen 2 上工作。相同的错误可能会在当前的测试版中持续存在,但我们修复它可能不会太久。

最后我找到了基于 Ilia Yatchev 答案的解决方案,非常感谢他。

首先我使用下载的 jaydata 文件而不是在线文件,因为它包含 Ilia Yatchev 在他的回答

中提到的代码更改
var Class;
$data.Class = Class = new ClassEngineBase();

注意:订单现在不是问题

<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>

然后在代码中我们保存创建实体的引用:

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 }
});

然后我们保存创建的上下文的引用:

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

然后我们就可以安全地处理这些引用了:

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) });
    });
});

如果我不保存对实体和上下文的引用,我会收到此错误

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)