从Marionette ItemView中获取骨干模型

时间:2014-02-20 07:35:06

标签: javascript backbone.js marionette

我有以下Marionette模块,主要是功能。

目前我想要做的是在输入框中按下“Enter”键时从服务器获取模型。为了简单起见,我将Postcode模型ID硬编码为3215,我知道我已经设置了id。就目前而言,我运行这个样本,它从/ postcode获取,而不是我想要的/ postcode / 3215。我检查了属性,id肯定是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
    });
  });

});

1 个答案:

答案 0 :(得分:1)

你的问题在这里:

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