0

有很多这样的问题。我还没有找到答案。

使用带有主干.js 和 require.js 的示例 TodoMVC,我想从服务器而不是本地存储中获取。

我有一个返回正确 json 集合的 url,其中的模型是这样的:

{"string1": "foo", "string2":"bar", "somefloat":0}

在我的模型(model/todo.js)中,我将默认值更改为:

defaults: {string1: '', string2: '',somefloat: 0},

在我的收藏(collections/todos.js)中,我注释掉 localstorage 并添加一个 url。

这使得 fetch 转到我的服务器,我可以看到它返回了 json 集合。

但由于某种原因,模型在主干.js 行 817 中未定义

    // Prepare a model or hash of attributes to be added to this collection.
    _prepareModel: function (model, options) {
        options || (options = {});
        if (!(model instanceof Model)) {
            console.log(Model);
            var attrs = model;
            console.log(attrs.Kana);
            options.collection = this;

            //ERROR IN THIS LINE: Uncaught TypeError: undefined is not a function 
            model = new this.model(attrs, options);

            if (!model._validate(model.attributes, options)) model = false;
        } else if (!model.collection) {
            model.collection = this;
        }
        return model;
    },

为什么我不使用本地存储时模型未定义?

4

1 回答 1

0

您还需要urlRoot在模型中设置属性:

var TodoModel = Backbone.Model.extend({

    urlRoot: "/Practice/GetCollection",
于 2012-10-04T13:28:12.943 回答