0

我已经尝试了骨干教程中的简单示例,但无法正常工作

var Person = Backbone.Model.extend({});

var People = Backbone.Collection.extend({
    model: Person,
    url: "http://localhost:3002/people"
});

var people = new People();

var person = new Person({
    id: 3
});

person.fetch({
    success: function () {
        person.set({age: 23});
        person.save();
    }
});

我只想更新 id 等于 3 的现有记录,但出现错误A "url" property or function must be specified。我确信我在输入这个例子时没有犯错,但它在教程中有效,对我不起作用。是不是因为一些版本的变化?

4

1 回答 1

0

正如错误和评论所表明的那样,您需要url为模型指定属性或将person模型添加到people集合中。

如果您想将fetch您的模型使用与urlpeople集合相同。您需要通过执行以下操作添加personpeople集合中:

var people = new People(person);

// or 

people.add(person);

// The fetch url for a person would look like
// GET http://localhost:3002/people/3       Assuming the id of the person is 3.

如果您需要使用不同于url您的集合指定的person模型。您可以在模型中指定urlorurlRoot属性。Person

var Person = Backbone.Model.extend({
  urlRoot:'http://localhost:3002/person'
});

// The fetch url for a person would look like
// GET http://localhost:3002/person/3       The number will match id of model.
于 2013-09-07T21:53:38.613 回答