Search code examples
jquerybackbone.jschaplinjs

Backbone Model missing url param ... not


I am trying to to sync a model with server. Unfortunately, despite setting url and rootUrl on the model, I still get url property isn't specified.

Needless to say I can do model.fetch() (GET) with this model just fine, but when attempting to POST I suddenly lose URL.

>>> model = window.mod
ClientSchema { cid="c2", attributes={...}, _changing=false, more...}
>>> model.url
"http://localhost:8080/mp/add"
>>> model.urlRoot
"http://localhost:8080/mp/add"
>>> model.set({test:2})
ClientSchema { cid="c2", attributes={...}, _changing=false, more...}
>>> model.sync()
Error: A "url" property or function must be specified
urlError()vendor.js 
Backbone.sync()vendor.js 
.sync()vendor.js 
throw new Error('A "url" property or function must be specified');

Model

# coffeescript
# Chaplin.Model just extends Backbone.Model
module.exports = class ClientSchema extends Chaplin.Model
    url: 'http://localhost:8080/mp/add'
    urlRoot:'http://localhost:8080/mp/add'

Model.sync

>>> model.sync.toString()

"function () {
      return Backbone.sync.apply(this, arguments);
    }"    

Solution

  • if you did not overwrite the default sync method. you will need to pass either a model or an url.

    this is the original sync:

    Backbone.sync = function(method, model, options) {
    ...
      if (!options.url) {
        params.url = _.result(model, 'url') || urlError();
      }
    ...
    }
    

    when you call model.sync() you are not passing anything.