2017-03-04 26 views
0

很新的骨幹,所以請介意我的無知,骨幹取 - 「URL」屬性或功能必須在指定的錯誤

我有一個簡單GallereyModel:

var GalleryModel = Backbone.Model.extend({ 
    defaults : { 
     id: "", 
     width: "" 
    }, 
    url : "" 
}); 

當模型更新,功能galleryModelUpdate被稱爲

var GalleryView = Backbone.View.extend({ 

initialize: function() { 
    this.listenTo(this.model, "change", this.galleryModelUpdate); 
}, 
galleryModelUpdate: function(){ 
    console.log("updated gallery model"+this.model.get("id"); 
    if (this.model.get("url")){ 
     console.log("fetching " + this.model.get("url")); // this line prints with the correct url 
     this.model.fetch({ // this line gives error 
      success: function(){ 
       console.log("fetched succesfully!"); 
      } 
     }); 
    } 
} 

});

我在調用fetch之前在模型上打印url的值,所以不確定爲什麼拋出「url」未定義的錯誤?爲了被定義爲模型來獲取

提前感謝您的幫助

+0

但你不設置'url'在獲取 - 'this.model.fetch({ 網址:this.model.get (「url」), 成功:function(){ console.log(「fetched succesfully!」); } });' –

+0

哦,我明白了。我感覺'fetch'調用會自動在模型中尋找'url'。謝謝,解決了問題 – user2953828

+1

Backbone會查看'url' *屬性*而不是*屬性*。你的url:「」'是一個屬性,'this.model.get('url')'是屬性,但它們不相關:http://stackoverflow.com/a/39820057/479863 –

回答

1

無論是urlurlRoot模特屬性的需求。 您可以將模型的網址指向的URL數據屬性:

Backbone.Model.extend({ 
    url: function() { 
    return this.get('url'); 
    } 
}); 
相關問題