Backbone View没有初始化

时间:2013-02-18 12:21:51

标签: javascript jquery backbone.js underscore.js

我是骨干js的绝对初学者,并一直试图遵循this GIT repo的路线。以下是我放入的最低限度代码。

(function() {

var MWLivePreview = {};
window.MWLivePreview = MWLivePreview;

var MWTemplate = function(name) {
    return _.template($("#" + name + "-template").html());
}

var log = function(logit) {
    console.log(logit);
}

MWLivePreview.Index = Backbone.View.extend({
    template: MWTemplate('live-preview'),
    render: function() {
        log(this.template(this));
        this.$el.html(this.template(this));
        return this;
    }
});

MWLivePreview.Router = Backbone.Router.extend({
    initialize: function(options) {

        this.el = options.el
    },

    routes: {
        "": "index"
    },

    index: function() {
        var view = new MWLivePreview.Index();

        this.el.empty();
        this.el.append(view.render().el);
    }
});

MWLivePreview.boot = function(container) {
    container = $(container);
    var router = new MWLivePreview.Router({el: container});
    Backbone.history.start();
}

})()

以下代码是我的模板:

<script type="text/template" id="live-preview-template">
    <div> We have got few templates</div>
</script>

我通过在文档就绪

上调用下面的代码来整理整个事情
MWLivePreview.boot($("#asapatterns"));

我不确定我哪里出错但这会返回以下错误:

Uncaught TypeError: Object function (a){return new m(a)} has no method 'pick' 

关于可能出错的任何想法或线索?

编辑1:

删除Backbone.history.start()会停止提供错误,但视图中没有任何内容。

1 个答案:

答案 0 :(得分:1)

来自http://backbonejs.org/

  

Backbone唯一的硬依赖是Underscore.jsLo-Dash

但要小心匹配Backbone所需的版本:在撰写本文时,对于Backbone 0.9.10,使用underscore.js 1.4.3