簡體   English   中英

骨干RESTful JSON模式

[英]Backbone RESTful JSON schema

如果我想將以下數據呈現給ribs.js集合,我創建的JSON應該是什么樣的?:

id: 1
fname: "John"
surname: "Lennon"
email: "john@beatles.com"
age: 22

id: 2
fname: "Paul"
surname: "McCartney"
email: "paul@beatles.com"
age: 22

id: 3
fname: "George"
surname: "Harrison"
email: "george@beatles.com"
age: 20

id: 4
fname: "Ringo"
surname: "Starr"
email: "ringo@beatles.com"
age: 24

我一直將其導出如下:

[{
    "id":1,
    "fname":"John",
    "surname":"Lennon",
    "email":"john@beatles.com",
    "age":22
},{
    "id":2,
    "fname":"Paul",
    "surname":"McCartney",
    "email":"paul@beatles.com",
    "age":22
},{
    "id":3,
    "fname":"George",
    "surname":"Harrison",
    "email":"george@beatles.com",
    "age":20
},{
    "id":4,
    "fname":"Ringo",
    "surname":"Starr",
    "email":"ringo@beatles.com",
    "age":24
}]

當使用上述JSON呈現時,我的收藏似乎只包含最終的甲殼蟲(Ringo)。


這是我的觀點:

var app = app || {};

app.BeatleView = Backbone.View.extend({

        el: '#page',

        template: Handlebars.getTemplate( 'account_statement' ),

        initialize: function() {
                console.info('init:',this.collection);
                this.render();
                this.listenTo(this.collection, 'add', this.render);
                this.listenTo(this.collection, 'reset', this.render);
                this.collection.fetch();
        },

        // render library by rendering each book in its collection
        render: function() {
                var data = this.collection.toJSON();
        console.log('col', this.collection );  // added
                this.$el.html( this.template( {beatles: data} ));
                return this;
        }
});

這是我的收藏

var app = app || {};

app.BeatlesCollection = Backbone.Collection.extend({
        model: app.Beatle,
        url: 'http://localhost/path/to/beatles',

        initialize: function() {
                console.log('Init Collection');
        }
});

這是我的模特

var app = app || {};

// create a model to represent a single transaction on a statement
app.Transaction = Backbone.Model.extend({});

這就是console.log('col', this.collection ); 在我的視圖的render方法中顯示:

col child {length: 1, models: Array[1], _byId: Object, _listenerId: "l2", _events: Object…}
_byId: Object
_events: Object
_listenerId: "l2"
length: 1
models: Array[1]
0: child
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
amount: 205.99
currency: "USD"
date: "2013-05-13"
id: 13
vendor: "Reebok Outlet"
__proto__: Object
changed: Object
cid: "c3"
collection: child
id: 13
__proto__: Surrogate
length: 1
__proto__: Array[0]
__proto__: Surrogate

我的車把模板如下所示:

<h1>Your Statement</h1>
<table border="1">
    <thead>
        <th>Name</th>
        <th>Email</th>
        <th>Age</th>
    </thead>
    <tbody>
    {{#each beatle}}
        <tr>
            <td>{{this.fname}} {{this.surname}}</td>
            <td>{{this.email}}</td>
            <td>{{this.age}}</td>
        </tr>
    {{/each}}
    </tbody>
</table>

我上面發布的JSON似乎正確。

問題是我發布的JSON是偽代碼,而我實際上從后端系統輸出的JSON是錯誤的-輸出中的所有ID都是相同的數字(復制/粘貼問題)。

對於希望創建Backbone / Handlebars應用程序的任何人,上面的代碼似乎都是一個不錯的起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM