I'm trying to make a new Backbone model (working within Knockback) and I'm currently trying to set it up with a RESTful backend server. The issue is that the URL isn't accepted when trying to use the objectives.sync(). It works normally when doing objectives.fetch() however, and correctly pulls the data from the specified URL. What am I doing wrong here?
/**
* 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) {}
});
The code to actually make use of this collection can be seen here:
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();
});/**/