我正在构建一个小程序,以显示一组人的一些细节。可以选择更新详细信息。但是,视图不会呈现更新:
这是我的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();
我想看到表格中第4个名字的实时更新。当我在文本框区域进行更改时,Sally Fox。同样地,当我在单选按钮上进行更改时,我希望看到Sally Fox的表性别发生变化。
基本上我希望我的Backbone.TableView能够监听Backbone.UI.RadioGroup和Backbone.UI.TextField等其他Backbone.UI小部件所做的更改
答案 0 :(得分: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,我们可以通过这种方式取得进展。
答案 1 :(得分:0)
虽然Peter(上图)给出的答案也是正确的,但我们也可以直接在backbone-ui.js库文件中进行更改:
在tableview的initialize方法内部(在文件backbone-ui.js中搜索tableview,找到它的初始化函数)添加:
this.model.on("change", this.render, this);
你会没事的。 (您可能希望在backbone-ui.js文件的list属性的initialize方法中添加相同的行以获取列表的响应视图)