简体   繁体   English

Backbone.View不响应Backbone.UI

[英]Backbone.View not responding to Backbone.UI

I'm building a small program to allow display of some details of a group of people. 我正在构建一个小程序,以允许显示一群人的一些细节。 There is an option to update the details. 有一个选项可以更新详细信息。 However, the updates are not being rendered by the view: 但是,视图不会呈现更新:

Here is my server.js: 这是我的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);

My index.jade is: 我的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")

My theapp.js file is: 我的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();

I want to see real-time update of 4th name in the table viz. 我想在表viz中看到第4个名称的实时更新。 Sally Fox when I make changes in the textbox area. 当我在文本框区域中进行更改时,Sally Fox就会出现。 Similarly I would like to see the table gender of Sally Fox change when I make the change in the radio button. 同样,当我在单选按钮中进行更改时,我希望看到Sally Fox的表性别发生更改。

Basically I want my Backbone.TableView to listen to changes made in other Backbone.UI widgets like Backbone.UI.RadioGroup and Backbone.UI.TextField 基本上,我希望Backbone.TableView可以监听其他Backbone.UI小部件(例如Backbone.UI.RadioGroup和Backbone.UI.TextField)中所做的更改

You're on the right path. 您在正确的道路上。 Add listeners in your views to listen for when changes happen to the people collection. 在视图中添加侦听器,以侦听people集合发生更改时的情况。 Changes to individual models (ie sally ) will also bubble up through the collection. 对单个模型的更改(即sally )也将在整个集合中冒泡。

I'm not too familiar with Backbone.UI, but the changes would go something like this: 我对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);
    }
}); 

You would do similar things for other views that you want to react to data changes. 您将对要对数据更改做出反应的其他视图执行类似的操作。 The key is to have the views share the same instance of the model/collections you're changing, and then listen for specific changes. 关键是让视图共享您要更改的模型/集合的相同实例,然后侦听特定的更改。

If this doesn't get you going, post comments and/or a JSFiddle and we can make progress that way. 如果这不能使您继续前进,请发表评论和/或JSFiddle,我们可以这样取得进展。

While the answer given by Peter (above) is also correct, we can also make the changes directly in the backbone-ui.js library file: 尽管彼得给出的答案(上述)也是正确的,但我们也可以直接在ribs-ui.js库文件中进行更改:

Inside the initialize method for tableview (search for tableview in the file backbone-ui.js, find its initialize function) add: 在tableview的initialize方法内(在文件ribone-ui.js中搜索tableview,找到其initialize函数)添加:

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

and you'll be fine. 你会没事的。 (You may want to add the same line inside the initialize method of the list property of backbone-ui.js file to get responsive views for lists) (您可能希望在ribs-ui.js文件的list属性的initialize方法内添加同一行,以获取列表的响应视图)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM