backbone.js绑定的问题

时间:2012-07-25 12:04:03

标签: javascript binding backbone.js

我是骨干新手并尝试通过绑定获取模型更新来更新我的HTML,但没有运气。我正在使用jquery.window插件来创建弹出窗口。检查我按更改标题按钮并更新我的模型标题,html没有得到更新。非常感谢任何帮助,谢谢

埃里克

这是我的代码

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>I have a back bone</title>
</head>
<body>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js"></script>
    <script src="js/jquery.window.js"></script>
    <script src="js/underscore.js"></script>
    <script src="js/backbone.js"></script>
    <script src="js/app.js"></script>
    <link rel="stylesheet" href="css/main.css">
    <link rel="stylesheet" href="css/jquery.window.css">

    <button id="add-window">Add Window</button>
    <button id="change-title">Change Title</button>


    <script type="text/template" id="search_template">
        <label><%= title %></label>
        <input type="text" id="search_input" />
        <input type="button" id="search_button" value="Search" />
    </script>

</body>
</html>

使用Javascript:

(function($) {

    AppWindow = Backbone.Model.extend({
        title : "John",
        window : null       
    });

    AppWindows = Backbone.Collection.extend({
        initialize : function(models, options) {
            this.bind("add", options.view.addWindowView);
        }
    });

    WindowView = Backbone.View.extend({

        initialize : function(){
            _.bindAll(this, "render");
            this.model.bind("change:title");
            this.template = _.template($("#search_template").html());
            this.render().el;
        },

        render : function(){
            var formString = this.template(this.model.toJSON());
            var win = $.window({
                icon : 'http://www.fstoke.me/favicon.ico',
                title : this.model.get("title"),
                content : formString,
                x : 80,
                y : 80,
                resizable : true,
            });
            this.el = $("#" + win.getWindowId()+".window_frame");
            this.delegateEvents(this.events);
            return this;
        }

    });

    AppView = Backbone.View.extend({
                views: {},
                el : $("body"),
                initialize : function() {
                    this.appWindows = new AppWindows(null, {
                        view : this
                    });
                },

                events : {
                    "click #add-window" : "addWindow",
                    "click #change-title" : "changeTitle"
                },

                addWindow : function() {
                    var window_title = "Eric";
                    var window_model = new AppWindow({
                        title : window_title
                    });
                    // Add a new model to our friend collection
                    this.appWindows.add(window_model);
                },

                changeTitle : function(){
                    var win = this.appWindows.at(0);
                    win.set("title", "jamjamajm");
                },

                addWindowView : function(model) {
                    var view = new WindowView({
                        model : model
                    });
                    model.set("window", view);
                },

                deleteWindow : function(wnd){
                    console.log("deleteing");
                }
            });

    var appview = new AppView;
})(jQuery);

1 个答案:

答案 0 :(得分:1)

您忘记为change:title模型的WindowView绑定添加方法。

Backbone.js文档说明了关于on - 函数(又名绑定)的以下内容:

  

object.on(event,callback,[context])别名:bind

     

将回调函数绑定到对象。只要事件被触发,就会调用回调。

因此,对于this.model.bind("change:title");,您刚刚提供了每次更改属性title时触发的事件。您还没有提供回调函数,因此在触发事件时没有任何反应。我认为你希望回调是WindowView的渲染函数,所以这应该解决它:

this.model.on("change:title", this.render); // changed to on as bind is deprecated but still supported

这样,无论何时触发事件change:title,都会调用与该模型对应的render的{​​{1}}。