1

バックボーンJSを使用していて、モデルからのデータを表示するのに問題があります。誰でもこれがどのように機能するかを知ることができますか? ティア

VIEW.JS -

    render: function() {
             ///EDITED TO SIMPLIFY      

            var toModel = new tModel();//tModel is the name of the model    
    console.log(toModel.get('data'));//?? UNDEFINE


  }

モデル.JS --

    data:[
        { text: "Google", href: "http://google.com" },
        { text: "Facebook", href: "http://facebook.com" },
        { text: "Youtube", href: "http://youtube.com" }
    ],  
4

2 に答える 2

2

http://jsfiddle.net/g3U7j/6/

model.js

MyModel = Backbone.Model.extend({
    defaults: {
        data: [
            {
            text: "Google",
            href: "http://google.com"},
        {
            text: "Facebook",
            href: "http://facebook.com"},
        {
            text: "Youtube",
            href: "http://youtube.com"}
        ]
    }
});

view.js

MyView = Backbone.View.extend({
    initialize: function() {
        var x = this.model.get('data');
        console.log(x);
    }
});

var View = new MyView({
    model: new MyModel
});​
于 2012-09-11T09:25:20.620 に答える
0

それはthis.model.get('data')(インスタンスメンバーではなくクラスを参照するModelの大文字Mを削除します)です。

また、イベント ハンドラーから直接呼び出す場合は、render メソッドでクラス インスタンスに適切にバインドされているrenderことを確認する必要があります (javascriptの代わりに使用する通常のもの。私はこれを主に Coffeescript で行っており、少し簡単です) )。thisthatthis

于 2012-09-11T09:07:28.763 に答える