1

我正在尝试掌握 Backbone.js,并一直遵循Thomas Davis在此处概述的“模块化”方法。

尝试在另一个视图中“包含”一个视图时,我似乎被卡住了,如下所示:

设置视图.js

define([
  'jquery',
  'underscore',
  'backbone',
  'text!templates/settings/settingsTemplate.html'
], function($, _, Backbone, settingsTemplate){   

  var SettingsView = Backbone.View.extend({
    el: $("#interface"),

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

  });

  return SettingsView;

});

ScriptView.js

define([
    'jquery', 
    'underscore', 
    'backbone',
    'views/settings/SettingsView',
    'models/script/ScriptModel', 
    'collections/scripts/ScriptsCollection',
    'text!templates/script/scriptTemplate.html'
],    
    function($, _, Backbone, SettingsView, ScriptModel, ScriptsCollection,  scriptTemplate) {

    var ScriptView = Backbone.View.extend({
        el : $("#container"),

        render : function() {
            this.$el.html(scriptTemplate);

            //add the settings view
            var settingsView = new SettingsView();
            settingsView.render();
        }
    });

    return ScriptView;
});

更新:我已经设法修复了我刚刚意识到为什么会发生这种情况的错误(参数的顺序错误 - DOH !!)

我不再收到错误消息,但我的“SettingsView”仍然没有出现。当我在“ScriptView.js”中进行控制台记录时,我看到“el”是未定义的,所以我的想法是这可能是问题所在......?

谢谢

4

1 回答 1

1

我认为您必须将附加SettingsViewelScriptView

var ScriptView = Backbone.View.extend({
    el : $("#container"),

    render : function() {
        this.$el.html(scriptTemplate);

        //add the settings view
        var settingsView = new SettingsView();
        settingsView.render();

        this.$el.append(settingsView.el);
    }
});

一篇关于子视图的好帖子就是这个

我希望它有帮助!

于 2013-10-14T11:32:22.360 回答