Comments (14)
So the resource_uri
is expected to be used as the id
and it should be the full URI of the model. I do not understand this design, could you explain? Thank you.
from backbone-tastypie.
Well, that the url is just /books
for a new object is entirely correct, isn't it? If you save it, you should do a POST request to /books
, and Tastypie will return you an object containing a resource_uri
.
from backbone-tastypie.
What if it is a exist object I want to get? Say, the id
is from the URL.
from backbone-tastypie.
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 resource_uri
. I've never encountered a situation where I'd have an id
, but not the resource_uri
?
from backbone-tastypie.
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.
from backbone-tastypie.
I concur with Nathan. just using id is very much helpful in many cases.
from backbone-tastypie.
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 resource_uri
(if you did, there wouldn't be a problem fetching it, right?). If that's the case, it's possible you do already have an id
in some way that can be used with the urlRoot
to construct a complete url from.
If that's correct, that would be pretty easy to add.
from backbone-tastypie.
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.
http://documentcloud.github.com/backbone/#Model-urlRoot
from backbone-tastypie.
Thanks, but I know Backbone quite well ;). I'm just trying to clarify how the behavior for url
should be in relation to the model's id
attribute and Tastypie's resource_uri
. So if the following sounds okay, this should be pretty trivial to fix:
- Normally (when requesting data from the server), the current behavior (constructing a url from the
resource_uri
) is fine; - however, there are cases where you do have an actual, existing (hopefully ;)
id
attribute but the model doesn't contain aresource_uri
; - in these cases, it'd be nice if the
url
function could just use theid
attribute (so, usemodel.get('id')
as opposed tomodel.id
) and attach it to theurlRoot
.
from backbone-tastypie.
thats correct. Thanks Paul!
from backbone-tastypie.
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 :/
from backbone-tastypie.
I don't see how it wouldn't work, as long as you a) set the urlRoot
to the proper value, and b) leave idAttribute
pointing to resource_uri
(as it should be for tastypie?).
(btw, both getUrl
and urlError
are functions in Backbone's local scope, as far as I know)
from backbone-tastypie.
Have you tested it yourself?
from backbone-tastypie.
Well, yes? See tests.js
:
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.
from backbone-tastypie.
Related Issues (20)
- Can't get at attributes from fetchRelated() model HOT 1
- Problem accessing Models in Collection HOT 1
- Problem saving a model related to another model HOT 3
- ApiKey isn't sent with extra GET during Backbone.sync HOT 6
- PUT is handled as a POST instead, resulting in server error HOT 2
- Collections contain unused meta data, limiting collection results HOT 3
- Delete method in tastypie returns an empty 204 response and this doesn't play nice with backbone. HOT 7
- Set CSRF token from cookie HOT 2
- Bower support HOT 1
- This should be NPM installable and not a Django App HOT 6
- Please tag the current release (0.2) so that this can be added to cdnjs.com HOT 1
- How does it work? HOT 2
- Error callback is always called with raw JSON response HOT 1
- options.headers undefined when trying to sync relational model HOT 1
- don't override the default idAttribute HOT 1
- [enhancement] Add missing bower.json. HOT 1
- Include django example app please HOT 1
- DELETE change url
- Wrong "main" value in bower.json HOT 2
- Typo in package.json
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backbone-tastypie.