2013-04-19 32 views
0

好吧,這可能不太清楚。骨幹 - 在視圖中訪問模型默認值通過集合

我將一個集合傳遞給了我的視圖。我的集合有一個Model,而Model有一個默認的數組。當我從視圖中記錄集合時,它顯示沒有長度。但是我的模型中有4個默認值。我如何獲得我的模型的默認值到我的視圖?

查看通話:

var menuLinks = new App.Collections.MenuLinks ; 
var newView = new App.Views.Navbar({ collection: menuLinks }) ; 

查看:

App.Views.Navbar = Backbone.View.extend({ 

    initialize: function(){ 

    console.log(this.collection) ; 
    //this.render() ; 

    } 

}); 

集合:

App.Collections.MenuLinks = Backbone.Collection.extend({ 

model: App.Models.MenuLinks 

}); 

型號:

App.Models.MenuLinks = Backbone.Model.extend({ 

//Default menus 
defaults:[ 
    { 
     name: 'Home', 
     href: '' 
    }, 
    { name: 'Trips', 
     href: '#trips' 
    }, 
    { name: 'Login', 
     href: '#login' 
    }, 
    { name: 'LogoutOhYeah', 
     href: '#logout' 
    }, 
] 

}); 

回答

0

所以...你只需要像這樣的東西:

var collection = this.options.collection, defaults; 
defaults = collection.length && collection.models[0].defaults ||{};