kelso
kelso

Reputation: 394

Fetch a backbone model from a Marionette ItemView

I have the following Marionette module, that is mostly functional.

Currently what I want to do is to fetch the model from the server when the "Enter" key is pressed in the input box. For simplicities sake, I am hardcoding the Postcode model id to 3215, just so I know I have set the id. As it stands, then I run this sample, it goes to fetch from /postcode, rather than /postcode/3215 which I want. I have checked attribute, and the id is definitely 3215.

var SearchModule = App.module("SearchModule", function(Mod, App, Backbone, Marionette, $, _) {

  var Postcode = Backbone.Model.extend({
    url: '/postcode'
  });

  var SearchView = Marionette.ItemView.extend({
    template: "#searchTemplate",
    ui : {
      input: ".typeahead"
    },
    events: {
      "keypress":"keypress"
    },

    keypress: function (e) { 
      if(e.which == 13) {
        var model = new Postcode();
        model.set("id", 3215);
        console.log(model); 
        model.fetch();
      }
    }
  });


  var SearchController = Marionette.Controller.extend({
        initialize: function (options) {
            this.region = options.region;
            var view = new SearchView();
            this.region.show(view);
        }
  });


  Mod.addInitializer(function () {
    Mod.controller = new SearchController({
        region: App.search
    });
  });

});

Upvotes: 0

Views: 164

Answers (1)

Rida BENHAMMANE
Rida BENHAMMANE

Reputation: 4129

Your problem is here :

var Postcode = Backbone.Model.extend({
    urlRoot: '/postcode' // urlRoot not url
});

Upvotes: 1

Related Questions