GithubHelp home page GithubHelp logo

Comments (14)

chengyin avatar chengyin commented on May 13, 2024

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.

PaulUithol avatar PaulUithol commented on May 13, 2024

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.

chengyin avatar chengyin commented on May 13, 2024

What if it is a exist object I want to get? Say, the id is from the URL.

from backbone-tastypie.

PaulUithol avatar PaulUithol commented on May 13, 2024

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.

nathanborror avatar nathanborror commented on May 13, 2024

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.

chachra avatar chachra commented on May 13, 2024

I concur with Nathan. just using id is very much helpful in many cases.

from backbone-tastypie.

PaulUithol avatar PaulUithol commented on May 13, 2024

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.

chachra avatar chachra commented on May 13, 2024

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.

PaulUithol avatar PaulUithol commented on May 13, 2024

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 a resource_uri;
  • in these cases, it'd be nice if the url function could just use the id attribute (so, use model.get('id') as opposed to model.id) and attach it to the urlRoot.

from backbone-tastypie.

chachra avatar chachra commented on May 13, 2024

thats correct. Thanks Paul!

from backbone-tastypie.

nathanborror avatar nathanborror commented on May 13, 2024

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.

PaulUithol avatar PaulUithol commented on May 13, 2024

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.

nathanborror avatar nathanborror commented on May 13, 2024

Have you tested it yourself?

from backbone-tastypie.

PaulUithol avatar PaulUithol commented on May 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.