Code Monkey home page Code Monkey logo

Comments (8)

SpaceK33z avatar SpaceK33z commented on June 12, 2024

What would you expect the output of console.log to be?

from backbone-relation.

mdamien avatar mdamien commented on June 12, 2024

I edited the content of the issue to make this clear, it should be undefined.

Here's a QUnit test for that, I'm taking a look at fixing it myself:

QUnit.test('doing an unset on a related model should remove it', 2, (assert) => {
    const mPost = new MPost();

    mPost.set('author', { id: 5, name: 'Burhan' });
    assert.equal(mPost.get('author').get('id'), 5);

    mPost.unset('author');
    assert.equal(mPost.get('author'), undefined, '.unset should make the model undefined');
});

from backbone-relation.

SpaceK33z avatar SpaceK33z commented on June 12, 2024

Hm, I'm not sure that is how backbone-relation should work. One of backbone-relation's strengths is that it keeps the same model instance. As a result of this, listening in a view this.listenTo(this.model.get('user), 'change:id', this.blaat); will always work.

Make sure you talk to @abzainuddin about this before fixing it.

from backbone-relation.

SpaceK33z avatar SpaceK33z commented on June 12, 2024

Oh, actually the output is incorrect, after an unset, the model should have the default values again.

from backbone-relation.

abzainuddin avatar abzainuddin commented on June 12, 2024

The manual gives:

This implies that once a relation is set, it cannot be overridden with another value. If you do really want to set another instance for a relation, then first use unset:

mAuthor.unset('user');
mAuthor.set('user', mUser);

The original idea was that unset is the escape where you can really unset a relation, so:

mPost.unset('author');

should remove the author.

from backbone-relation.

SpaceK33z avatar SpaceK33z commented on June 12, 2024

Ah whoops, remembered it wrong.
@mdamien, even if you don't manage to fix it yourself, could you add the failing test?

from backbone-relation.

mdamien avatar mdamien commented on June 12, 2024

Hm, I'm not sure that is how backbone-relation should work. One of backbone-relation's strengths is that it keeps the same model instance. As a result of this, listening in a view this.listenTo(this.model.get('user), 'change:id', this.blaat); will always work.

@SpaceK33z I think the problem you're describing is better solved by the ability to do "deep listening". Like:

this.listenTo(this.model, 'change:user.id', this.blaat);

from backbone-relation.

SpaceK33z avatar SpaceK33z commented on June 12, 2024

The problem I'm describing is already solved with backbone-relation. Using unset is still something you shouldn't do most of the times; instead better clear the model.

from backbone-relation.

Related Issues (1)

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.