错误:网址为"url"必须指定属性或函数,除非指定了url [英] Error: A "url" property or function must be specified, except url is specified

查看:68
本文介绍了错误:网址为"url"必须指定属性或函数,除非指定了url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话模型和一个显示该模型的视图.从服务器获取此模型没有任何问题(url属性可以正常工作),然后呈现视图. 但是,当我尝试在视图的函数中销毁模型时,出现错误必须指定"url"属性或函数",即使当我在destroy调用之前显示该url时,也正是网址应该是.

I have a Conversation model and a view that displays this model. This model is fetched from the server without any problem (the url property works fine then), and the view is rendered. However, when I attempt to destroy the model in a function of the view, I get the error 'A "url" property or function must be specified', even though when I display said url right before the destroy call, it is exactly the url it should be.

这是模型的代码:

MessageManager.models.Conversation = Backbone.Model.extend({
    defaults: {
        uid: '',
        title: '',
        messages: [],
        users: [],
        dateUpdated: null,
        isNew: true,
        message: ''
    },
    url: function(){
        var url = '/api/conversations';
        if(this.get('uid').length > 0) url += '/'+this.get('uid');
        return url;
    }
});

视图:

MessageManager.views.ConversationFull = Marionette.CompositeView.extend({
    template: this.template(MessageManager.templates.ConversationFull),
    childView: MessageManager.views.MessageListItem,
    childViewContainer: '#message-container',
    events: {
        'click a#btn-delete-conversation': 'deleteConversation'
    },
    deleteConversation: function(e){
        e.preventDefault();
        var self = this;
        console.log(self.model.url()); //This returns a correct url

        self.model.destroy({//This fires the error
            error: function(model, result, xhr){
                console.log(result.responseText);
            },
            success: function(model, response, options){
                MessageManager.conversations.sync();
                AMMain.router.pNavigate('welcome/');
            }
        });
    }
});

任何人都可以提供有关如何解决此问题的见解吗?我声明模型的方式有问题吗?

Can anyone give some insight on how to resolve this problem? Is there something wrong in the way I declare the model?

应该注意的是,即使原始获取没有问题,此模型上的其他调用(例如访存或同步)也会引发相同的错误.

It should be noted that other calls (like fetch or sync) on this model fire the same error, even though the original fetch works without a problem.

嗯,还没有完全脱离煎锅,但是我使用urlRoot和"id"属性更改了定义模型url的方式,现在DELETE请求被无误发送到服务器了. /p>

Well, not completely out of the frying pan yet, but I changed the way I defined the model's url, using urlRoot and the "id" attribute, and now the DELETE request is sent to the server without error.

推荐答案

defsq答案很好.唯一缺少的是在模型上定义idAttribute,因为它不是基于约定的id字段,而是uid.

defsq answer is good. The only thing missing is to define the idAttribute on your model, since it's not the convention-based id field, but uid.

MessageManager.models.Conversation = Backbone.Model.extend({
    idAttribute: 'uid',
    defaults: {
        uid: '',
        title: '',
        messages: [],
        users: [],
        dateUpdated: null,
        isNew: true,
        message: ''
    },
    urlRoot: "/api/conversations"
});

您无需手动添加"id".只需告诉Backbone您的id属性为uid,其他所有内容都将起作用.实际上,如果模型-event在内部存储为uid,则可以事件调用mymodel.id来获取模型-id的ID. :)

You don't need to append the "id" manually. Just tell Backbone that your id attribute is uid and everything else will work. In fact, you can event call mymodel.id to get the id of the model -event if it's stored internally as uid. :)

http://backbonejs.org/#Model-idAttribute

我个人不喜欢所有这些默认值.如果您未设置默认值,则所有内容都将为undefined,您可以使用if(未定义为falsey值)来简单地对其进行防范.

Personally I don't like all that default values. Everything will be undefined if you don't set default values, which you can simply guard against with an if (undefined is a falsey value).

if(!model.get("messages")){
   //no messages
}

这篇关于错误:网址为"url"必须指定属性或函数,除非指定了url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆