RequireJS和Breeze打得不好

时间:2015-07-21 14:20:20

标签: javascript requirejs breeze durandal

问题:

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 之前加载。所以我在这里完全糊涂了。

enter image description here

1 个答案:

答案 0 :(得分:1)

我在这里找到了答案: DataJS library not loading in RequireJS

显然,我们需要为同一个 js 文件引用2个单独的对象...如下所示:

'paths': {
    'datajs': '../datajs-1.1.3',
    'OData': '../datajs-1.1.3',
},
'shim': {
    'OData':['datajs']
}