2016-04-04 82 views
0

onsenui和jaydata不共存

<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 

任何帮助表示赞赏!

回答

0

最后,我发现一个解决方案基于Ilia Yatchev的答案,所以感谢他。

首先我用下载的jaydata文件,而不是网上的文件,因为它包含的代码更改,Ilia Yatchev对他的回答mentionned

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

好像都jaydata和onsenui使用window.Class,但是它们的实现显著不同。温泉使用约翰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; 
    }; 
})(); 

但是,我没有真正测试过它,如果它的工作,并且可能有一些问题。

  • 你是不是包括angular-onsenui.js,所以我想你使用温泉1,没有的温泉2. Here“萨略作修改的版本1.3.15不应该有冲突(不测试,抱歉)。

  • 在JayData他们存储$data.Class的类,所以事实证明,只有2个地方全局的是http://include.jaydata.org/jaydata.js

    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可能有更新版本的地方。对于温泉 - 温泉1的开发已经完成,我们只在温泉2上工作。同样的问题可能会持续到目前的测试版,但在我们修复它之前可能不会太长。
    +0

    我验证了jaydata.js文件,发现var Class;已存在 这里是我找到的代码 var Class; $ data.Class = Class = new ClassEngineBase(); 但基于这个建议,我发现了一个问题的解决方案: 通过添加引用我扩展的每个对象我将发布一个答案更多详细信息 谢谢! –

    +0

    很高兴我能帮上忙。我们可能会在温泉中解决这个问题,这样它就不会引起问题,但那将会在温泉2上。当我们解决它时,也许您可​​能有兴趣使用更新的版本:) –

    +0

    是的我会等待它,感谢您 –