0

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();
});/**/
moberemk
  • 1,597
  • 1
  • 18
  • 39

1 Answers1

1

Don't call sync() with no parameters like that. Sync expects arguments, which is why you are getting an error, but more to the point what you probably want to do is objective_model.save().

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274