-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot generate URL correctly #6
Comments
So the |
Well, that the url is just |
What if it is a exist object I want to get? Say, the |
If it's an existing object, wouldn't that mean you've fetched it from the server, or as a relation from another model? In that case, you'd have it's |
I do a lot of fetching with just ID's and this works in vanilla Backbone. I ended up removing your url and idAttribute overrides and everything worked just fine. |
I concur with Nathan. just using id is very much helpful in many cases. |
Could you give me an actual example maybe? Because I think I don't entirely understand the complete use case yet. My understanding at the moment is that sometimes you have models without a If that's correct, that would be pretty easy to add. |
Paul, See the backbone documentation. It lets you give a new object an id (meaning u have it in the backend, but in the frontend you are giving an id to tell it how to construct url and fetch it) etc. |
Thanks, but I know Backbone quite well ;). I'm just trying to clarify how the behavior for
|
thats correct. Thanks Paul! |
…xplicit 'id' attribute, in the absence of a 'resource_uri'. Fixes issue #6.
This doesn't appear to fix the problem. When .url() is called now it just returns the id of the model instead of the full url. What if the override was something like: Backbone.Model.prototype.url = function() {
// If there's no idAttribute, use the 'urlRoot'. Fallback to try to have the collection construct a url.
// Explicitly add the 'id' attribute if the model has one.
if (!this.id) {
url = this.urlRoot;
url = url || this.collection && (_.isFunction(this.collection.url)? this.collection.url(): this.collection.url);
if (url && this.has('id')) {
url = addSlash(url) + this.get('id');
}
url = url && addSlash(url);
return url || null;
}
else {
var base = getUrl(this.collection) || this.urlRoot || urlError();
if (this.isNew()) return base;
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + encodeURIComponent(this.id);
}
}; I just hacked this together, it could probably be optimized :/ |
I don't see how it wouldn't work, as long as you a) set the (btw, both |
Have you tested it yourself? |
Well, yes? See test("Model url", function() {
var person = new Person();
equal( person.url(), null );
// If the model doesn't have an 'urlRoot' and no value for it's 'idAttribute', the collection's
// 'urlRoot' is be used as a fallback ( a POST there creates a resource).
var coll = new Backbone.Collection();
coll.urlRoot = '/persons';
person.collection = coll;
equal( person.url(), '/persons/' );
// If present, the model's urlRoot is used as a fallback.
person.urlRoot = '/person';
equal( person.url(), '/person/' );
// The value of the explicit 'id' attribute is added to the 'urlRoot' when available
person.set( 'id', 2 );
equal( person.url(), '/person/2/' );
// If the idAttribute is set, it's used as the uri verbatim.
person.set( { resource_uri: '/person/1/' } );
equal( person.url(), '/person/1/' );
}); Could you check if these tests pass for you? If they do and you have a scenario that does fail, please share the testcase. |
I am not sure how this is supposed to work, but with the example code showed in the backbone.js document:
We get '/books' with
backbone-tastypie
. This is due to the overwritten onidAttribute
andurl
.The text was updated successfully, but these errors were encountered: