RequireJS 和 Breeze 表现不佳
RequireJS and Breeze not playing nicely
问题:
RequireJS 似乎不能很好地与 Breeze 搭配使用。
我的错误如下:
Unable to initialize OData. Needed to support remote OData services
这一般表示datajs没有加载,需要在breeze之前添加到页面中。但是,我已经使用 RequireJS 完成了此操作 - 或者至少我认为我已经完成了,但我的配置中可能有错误或缺失。
我的配置:
main.js 包含以下内容:
var paths = {
'text': '../text',
'durandal': '../durandal',
'plugins': '../durandal/plugins',
'transitions': '../durandal/transitions',
'breeze': '../breeze.debug',
'datajs': '../datajs-1.1.3',
'q': '../q',
};
var shim = {
'datajs': ['jquery'],
'breeze': ['jquery', 'datajs', 'q']
};
requirejs.config({
paths: paths,
shim: shim
});
测试模块(一个很简单的测试页面),如下:
JS:
define(['jquery', 'knockout', 'datajs', 'q', 'breeze'], function ($, ko, datajs, q, breeze) {
return {
people: ko.observableArray([]),
attached: function () {
breeze.config.initializeAdapterInstances({ dataService: "OData" });
var manager = new breeze.EntityManager('/odata');
var query = new breeze.EntityQuery()
.from("People")
.orderBy("Name asc");
manager.executeQuery(query).then(function (data) {
this.people([]);
$(data.httpResponse.data.results).each(function () {
var current = this;
this.people.push({ Id: current.Id, Name: current.Name });
});
}).fail(function (e) {
alert(e);
});
}
};
});
HTML:
<section>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody data-bind="foreach: people">
<tr>
<td data-bind="text: Id"></td>
<td data-bind="text: Name"></td>
</tr>
</tbody>
</table>
</section>
如您所见,我指定了 datajs 和 q 作为 breeze 的依赖项.那么,我在这里缺少什么?
编辑
我通过 FireBug 检查了 HTML,如您所见,q 和 datajs 似乎在 breeze 之前加载。所以我在这里完全糊涂了。
我在这里找到了答案:
DataJS library not loading in RequireJS
显然,我们需要为同一个 js 文件引用 2 个单独的对象...如下:
'paths': {
'datajs': '../datajs-1.1.3',
'OData': '../datajs-1.1.3',
},
'shim': {
'OData':['datajs']
}
问题:
RequireJS 似乎不能很好地与 Breeze 搭配使用。
我的错误如下:
Unable to initialize OData. Needed to support remote OData services
这一般表示datajs没有加载,需要在breeze之前添加到页面中。但是,我已经使用 RequireJS 完成了此操作 - 或者至少我认为我已经完成了,但我的配置中可能有错误或缺失。
我的配置:
main.js 包含以下内容:
var paths = {
'text': '../text',
'durandal': '../durandal',
'plugins': '../durandal/plugins',
'transitions': '../durandal/transitions',
'breeze': '../breeze.debug',
'datajs': '../datajs-1.1.3',
'q': '../q',
};
var shim = {
'datajs': ['jquery'],
'breeze': ['jquery', 'datajs', 'q']
};
requirejs.config({
paths: paths,
shim: shim
});
测试模块(一个很简单的测试页面),如下:
JS:
define(['jquery', 'knockout', 'datajs', 'q', 'breeze'], function ($, ko, datajs, q, breeze) {
return {
people: ko.observableArray([]),
attached: function () {
breeze.config.initializeAdapterInstances({ dataService: "OData" });
var manager = new breeze.EntityManager('/odata');
var query = new breeze.EntityQuery()
.from("People")
.orderBy("Name asc");
manager.executeQuery(query).then(function (data) {
this.people([]);
$(data.httpResponse.data.results).each(function () {
var current = this;
this.people.push({ Id: current.Id, Name: current.Name });
});
}).fail(function (e) {
alert(e);
});
}
};
});
HTML:
<section>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody data-bind="foreach: people">
<tr>
<td data-bind="text: Id"></td>
<td data-bind="text: Name"></td>
</tr>
</tbody>
</table>
</section>
如您所见,我指定了 datajs 和 q 作为 breeze 的依赖项.那么,我在这里缺少什么?
编辑
我通过 FireBug 检查了 HTML,如您所见,q 和 datajs 似乎在 breeze 之前加载。所以我在这里完全糊涂了。
我在这里找到了答案: DataJS library not loading in RequireJS
显然,我们需要为同一个 js 文件引用 2 个单独的对象...如下:
'paths': {
'datajs': '../datajs-1.1.3',
'OData': '../datajs-1.1.3',
},
'shim': {
'OData':['datajs']
}