yui / yui3

A library for building richly interactive web applications.
http://yuilibrary.com/
Other
4.12k stars 1.29k forks source link

LazyModelList fails to return getById when LML.model.idAttribute!=='id' #749

Closed ItsAsbreuk closed 11 years ago

ItsAsbreuk commented 11 years ago
    Y.UserModel = Y.Base.create('userModel', Y.Model, [], {
        idAttribute : 'userid'
    });

    var item1 = {
        userid: 1007,
        name: 'Marco Asbreuk',
    };

    var lazymodellist = new Y.LazyModelList();
    lazymodellist.model = Y.UserModel;

    lazymodellist.add(item1);

    var retreivedItem = lazymodellist.getById(1007);

    // retreivedItem --> null

See online example: http://jsbin.com/aqotob/1/edit

TeslaNick came with this patch: http://jsbin.com/aqotob/2/edit

which might be useful.

Marco

rgrove commented 11 years ago

From the LazyModelList docs:

Custom idAttribute fields are not supported.

http://yuilibrary.com/yui/docs/model-list/#lazymodellist

ItsAsbreuk commented 11 years ago

Hey, indeed..

I missed that.

Never considered to make it work though? I think it will be handy: I like LML - use it all the time. But now I run into serverdata with other unique id-fields...

Although I could transform all records to introduce 'id', but that would be a performancehit.

Marco.

rgrove commented 11 years ago

@ItsAsbreuk There's no reliable way to make custom id attributes work in LazyModelList. Relying on modelClass.prototype.idAttribute isn't a good solution since that prototype property may be overridden by a local property on an actual instance, and there's know way to know without creating an instance, which is exactly what LazyModelList is avoiding.

ItsAsbreuk commented 11 years ago

Ah, I understand. It makes me wonder why idAttribute is a prototype property and not an instanceproperty or an attribute. But there sure will be a good reason for that, so no need to argue.

Thx.

ItsAsbreuk commented 11 years ago

Of coarse... model needs to be a Class, no instance...

Still you could give LML an attribute (or instance-property) named 'idAttribute' and use that one. But my quess is you don't want to keep LML as light as possible :)