Backbone.js Model Sync声明URL未定义

时间:2013-05-24 19:45:47

标签: backbone.js backbone.js-collections knockback.js

我正在尝试创建一个新的Backbone模型(在Knockback中工作),我正在尝试使用RESTful后端服务器进行设置。问题是尝试使用objectives.sync()时不接受URL。它在执行objectives.fetch()时正常工作,并正确地从指定的URL中提取数据。我在这里做错了什么?

/**
 *  Objectives model
 */
var Objective = Backbone.Model.extend({
    url: 'api/objective',
    // Defaults
    defaults: {
        category: null,
        weight: null,
        name: null,
        descriptor: null
    }
});
/**
 *  Basic objectives collection
 */
var ObjectiveCollection = Backbone.Collection.extend({
    model: Objective,
    url: function() {
        return "api/objective";
    },
    initialize: function(models,options) {}
});

实际使用此集合的代码可以在这里看到:

var objectives = new ObjectiveCollection();
objectives.fetch();
var view_model = {
    objectives: kb.collectionObservable(objectives, {view_model: kb.ViewModel})
};
ko.applyBindings(view_model, $('#objectives').get(0));
// Listener for the click button
$('#click').click(function() {
    counter++;
    var objective_model = new Objective({name: Math.random(), descriptor: 'What up'});
    objectives.add(objective_model);
    objectives.sync();
});/**/

1 个答案:

答案 0 :(得分:1)

不要在没有类似参数的情况下调用sync()。同步期望参数,这就是你收到错误的原因,但更多的是你可能想要做的是objective_model.save()