Question

i have a problem with backbone.js. I'm creating a frontend for an existing api, for me unaccessable. The problem is that when I try to add a new model to a collection, i can see in my firebug that every time backbone tries to create the model it appends the attribute name to the url.

Example:

  1. default url = /api/database
  2. when i perform a GET = /api/database
  3. when i perform a GET/POST with object {"name": "test"} = /api/database/test is the result

Anyone knows how to avoid that behaviour?

Greetings Kern

My View:

window.databaseView = Backbone.View.extend({
  el: '#content',

  template: new EJS({url: 'js/templates/databaseView.ejs'}),

  initialize: function() {
    var self = this;
    this.collection.fetch({
      success: function() {
        console.log(self.collection);
        var test = self.collection.get("_system");
        console.log(test);
        self.collection.get("_system").destroy();
        self.collection.create({name: "test"});
      }   
    }); 
  },  

  render: function(){
    $(this.el).html(this.template.render({}));
    return this;
  }

});

Model:

window.Database = Backbone.Model.extend({
  initialize: function () {
    'use strict';
  },  

  idAttribute: "name",

  defaults: {
  }

});

Collection:

window.ArangoDatabase = Backbone.Collection.extend({
  model: window.Database,

  url: function() {
    return '../../_api/database/';
  },  

  parse: function(response) {
    return _.map(response.result, function(v) {
      return {name:v};
    }); 
  },  



  initialize: function() {
    this.fetch();
  },  

  getDatabases: function() {
    this.fetch();
    return this.models;
  },  

  dropDatabase: function() {

  },  

  createDatabse: function() {

  }
});
Was it helpful?

Solution

By default, Backbone create models URLs this way: {collection url}/{model id}.

It consider the collection URL to be a base URL in a RESTful way.

Here you only want to set the Model url property to the URL you whish to call. That'll overwrite the default behavior. http://backbonejs.org/#Model-url

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top