Backbone fetch - " url"属性或函数必须指定错误

时间:2017-03-04 12:32:19

标签: javascript backbone.js

Backbone很新,所以请注意我的无知......

我有一个简单的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"未定义的错误?

非常感谢您的帮助

1 个答案:

答案 0 :(得分:1)

需要定义urlurlRoot模型属性才能获取模型。 您可以将模型URL设置为指向url数据属性:

    public class ResourceAction extends ActionSupport {        
        private static final long serialVersionUID = -773740596147386242L;

        public String doSearchRequest() throws Exception {

            return "search";
        }
   }