Skip to content

HasOne not work, if key value is 0 #179

Closed
@rvinokurov

Description

if have some relation:

  models.Task = Backbone.RelationalModel.extend({

        relations: [
        
            {
                type: Backbone.HasOne, 
                key: 'scaner',
                relatedModel: 'models.Tool'
            }
        ]
});

and a have three scanner models with ids 0,1 and 2
Code work perfect if id > 0, and don't work, if id === 0
I find code of function "findRelated" in Backbone-relational and change it:

 findRelated: function( options ) {
            var item = this.keyContents;
            console.log(item);
            var model = null;

            if ( item instanceof this.relatedModel ) {
                model = item;
            }
            else if ( item || item === 0 ) { //add null check
                model = this.relatedModel.findOrCreate( item, { create: this.options.createModels } );
            }

            return model;
        }

Can you append my fix or another solution for fix bug, when id === 0?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions