0

我正在构建一个小程序来显示一组人的一些细节。有一个选项可以更新详细信息。但是,视图不会呈现更新:

这是我的 server.js:

var express= require("express"),
bodyparser= require("body-parser");

var app= express();

app.use(express.static(__dirname+ "/public"));
app.use(bodyparser.json());

app.get("/", function(req, res){
    res.render("index.jade");
});

app.listen(3002);

我的 index.jade 是:

doctype html

link(rel="stylesheet", href="backbui.css")

#main

script(src= "jquery.js")
script(src= "underscore.js")
script(src= "backbone.js")
script(src= "laconic.js")
script(src= "moment.js")
script(src= "backbui.js")
script(src= "theapp.js")

我的 theapp.js 文件是:

var Person= Backbone.Model.extend({}),
People= Backbone.Collection.extend({});

var Genders= new Backbone.Collection(
        [
             {name: "Female", id: 1},
             {name: "Male", id: 0}
]);

var people= new People([
            {name: "John Doe", gender: Genders.get(0), birthday:
             new Date(1990, 5, 15), married: false},
            {name: "Jane Smith", gender: Genders.get(1), birthday:
             new Date(1985, 10, 10), married: true},
            {name: "Tom Smith", gender: Genders.get(0), birthday: 
             new Date(1980, 1, 20), married: true},
            {name: "Sally Fox", gender: Genders.get(1), birthday: 
             new Date(1998, 7, 31), married: false}
]);

var table= new Backbone.UI.TableView({
    model: people,  // collection can also be passed
    columns: [
        {title: "Name", content: "name"},
        {title: "Gender", content: "gender", 
         format: function(model){ return model.get("name"); }},
        {title: "Birthday", content: "birthday",
         format: function(date){ return date.toString(); }},
        {title: "Married", content: "married",
         format: function(married){ return married? "yes": "no"; }}
    ]
}); 

var sally= people.models[3];

var textbox= new Backbone.UI.TextField({
    model: sally,
    content: "name",
});

var radios= new Backbone.UI.RadioGroup({
    model: sally,
    content: "gender",
    alternatives: Genders,
    altLabelContent: "name",
});

var Appview= Backbone.View.extend({
    el: "#main",

    render: function(){
            this.$el.append(table.render().el);
            this.$el.append(textbox.render().el);
            this.$el.append(radios.render().el);
            return this;
    }
});

new Appview().render();

我想在表格中看到第四个名字的实时更新。当我在文本框区域进行更改时,Sally Fox。同样,当我在单选按钮中进行更改时,我希望看到 Sally Fox 的表格性别发生变化。

基本上我希望我的 Backbone.TableView 监听其他 Backbone.UI 小部件(如 Backbone.UI.RadioGroup 和 Backbone.UI.TextField)中所做的更改

4

2 回答 2

1

你在正确的道路上。在您的视图中添加侦听器以侦听people集合何时发生更改。对单个模型(即sally)的更改也将通过集合冒泡。

我对 Backbone.UI 不太熟悉,但更改会是这样的:

// Make the table listen to changes in the passed-in collection & re-render
// when needed
var table= new Backbone.UI.TableView({
    model: people,  // collection can also be passed
    columns: [
        {title: "Name", content: "name"},
        {title: "Gender", content: "gender", 
         format: function(model){ return model.get("name"); }},
        {title: "Birthday", content: "birthday",
         format: function(date){ return date.toString(); }},
        {title: "Married", content: "married",
         format: function(married){ return married? "yes": "no"; }}
    ],

    initialize: function() {
        // When a change event happens to the model/collection, re-render
        this.listenTo(model, 'change', this.render);
    }
}); 

对于要对数据更改做出反应的其他视图,您可以执行类似的操作。关键是让视图共享您正在更改的模型/集合的同一实例,然后侦听特定更改。

如果这不能让您继续前进,请发表评论和/或 JSFiddle,我们可以通过这种方式取得进展。

于 2015-03-11T13:06:58.657 回答
0

虽然 Peter 给出的答案(上图)也是正确的,但我们也可以直接在主干 ui.js 库文件中进行更改:

在tableview的initialize方法里面(在文件backbone-ui.js中搜索tableview,找到它的initialize函数)添加:

this.model.on("change", this.render, this);

你会没事的。(您可能希望在主干 ui.js 文件的 list 属性的 initialize 方法中添加相同的行,以获取列表的响应式视图)

于 2015-03-12T19:21:02.840 回答