Skip to content
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

previous #101

Closed
pgherveou opened this issue Apr 2, 2012 · 4 comments
Closed

previous #101

pgherveou opened this issue Apr 2, 2012 · 4 comments
Labels

Comments

@pgherveou
Copy link
Contributor

Just noticed this little bug

first time you fetch a collection
model.previous('some-relation') will give you a string id
following change will return the related object. It would be good to always return either the id or the model, don't you think ?

@PaulUithol
Copy link
Owner

Hmm, yes. Seems like the Backbone.Model constructor just takes the plain attributes argument supplied when creating a model, and clones that into _previousAttributes.

By the way, for collections/HasMany relations this wouldn't really do much. _.clone makes a shallow copy, so unless you explicitly set a new collection, the previous collection will usually be the same as the one you get through get.

@PaulUithol
Copy link
Owner

Can't really seem to get this to surface. I don't know if the use of a collection or the fetching is a factor here, but using reset should be roughly equivalent:

    var User = Backbone.RelationalModel.extend(
        toString: function() {
            return this.get( 'username' );
        }
    );
    var Person = Backbone.RelationalModel.extend({
        relations: [{
            type: Backbone.HasOne,
            key: 'user',
            relatedModel: User
        }]
    });

    var PersonCollection = Backbone.Collection.extend({
        model: Person
    });

    var coll = new PersonCollection();
    var user = new User( { id: 3, username: 'root' } );
    coll.reset( [ { id: 2, user: 3 } ] );

    var person = coll.at( 0 );

    console.log(  person.get( 'user' ), person.previous( 'user' ) );
    // prints "root root"

@pgherveou
Copy link
Contributor Author

Ok I think my problem is that the document I fetch looks something like that if use person / user relation example
I presume that if the persons are loaded before the user, the references is not found in the dictionary and the relation not set

doc
|
|- users
|   |- Person
|   |   |- id
|   |   |- userId
|   |   |- attr1
|   |- Person
|   |   |- id
|   |   |- userId
|   |   |- attr1
|    
|- persons
|   |- User
|   |  |- id
|   |  |- attr1
|   |  |- attr2
|   |- User
|   |  |- id
|   |  |- attr1
|   |  |- attr2|

@pgherveou
Copy link
Contributor Author

Well apparently is linked to issue #91 my relation was not initialized correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants