backbone.js将集合绑定到表

时间:2014-02-18 08:31:25

标签: javascript json rest backbone.js

我得到了服务REST,它给了我一个这样的.JSON列表:

[{"name":"Temperature","id":32,"description":"Temperature on desk","lastModifier":"admin","lastModification":"2013-07-17"},
{"name":"Presence","id":34,"description":"maDescription2","lastModifier":"admin","lastModification":"2014-01-14"}]

我想使用3个attributs名称,description和lastModification在表格中显示此列表。

我试试这些解决方案:

http://www.codebeerstartups.com/2012/12/9-collection-views-in-backbone-js-learning-backbone-js/#.UwMVDH81jQc

http://liquidmedia.org/blog/2011/02/backbone-js-part-3/

但我只是不明白...... 这是我的代码:

模特:

    var sensors = Backbone.Model.extend({
    defaults: {
        name: "",
        id:"",
        description:"",
        lastModifier:"",
        lastModification:""
    },


});

收藏:

var SensorsCollection = Backbone.Collection.extend({
    model: sensors,
    url:'../../rest/groups'
});

观点:

var SensorsView = Backbone.View.extend({
    model: new sensors(),
    sensorsCollection: new SensorsCollection(),
    el: $("#sensorsDiv"),
    initialize: function(){

    },
    events:{
        "click #btnAddUser": "getSensors"

    },
    getSensors: function(){
        var tokenCookie;

        var name = "xtoken" + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) 
        {
            var c = ca[i].trim();
            if (c.indexOf(name)==0) tokenCookie = c.substring(name.length,c.length);
        }
        if(tokenCookie == undefined){
            alert("veuillez vous connecter !")
        }else{

            this.sensorsCollection.fetch({
                headers:{"X-Token":tokenCookie},
                statusCode:{
                    202: function(){

                    }
                }}

            );
        }
    }

});
var sensorsView = new SensorsView();

1 个答案:

答案 0 :(得分:0)

尝试按el: $("#sensorsDiv"),

更改el: "#sensorsDiv",

以下是如何使用模板Example

的示例