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

Collection: find #5

Closed
hrasoa opened this issue Oct 21, 2012 · 7 comments
Closed

Collection: find #5

hrasoa opened this issue Oct 21, 2012 · 7 comments

Comments

@hrasoa
Copy link

hrasoa commented Oct 21, 2012

var collection = new Epitome.Collection([
{
User: {name: 'Bob', id: 2}
},
{
User: {name: 'Angry Bob', id: 3}
}
]);

console.log(collection, collection.find('[User.name]'));

This actually doesn't work :( I am retrieving datas from CakePhp.

@krsmedlund
Copy link

I think you have misunderstood how this works, [User.name] isnt a valid selector for your data, you only have 2 attributes, id and name, you are picking all models that has the "User.name" attribute set,

console.log(collection, collection.find("[name]"));

will get all models with the name attribute, which is what you want.

console.log(collection, collection.find("[name=Bob]"));

will return only the first entry

@hrasoa
Copy link
Author

hrasoa commented Oct 21, 2012

Hi,
Thanks for the replay. I tried to use "[name=Bob]" or "[name]" as selector but it still returns an empty array.

@DimitarChristoff
Copy link
Member

yes, basically, you have an object that notates as `User.name' whereas the simple property selector is for properties at the level of User.

you can do a custom property getter 'name' in your model prototype that returns user.name or you can do

var myCollection = new Class({

    Extends: Epitome.Collection,

    findByName: function(who) {
        return this.filter(function(model){
            return model.get('User').name == who; 
        });
    }
});


var collection = new myCollection([{
User: {name: 'Bob', id: 2}
},
{
User: {name: 'Angry Bob', id: 3}
}
]);

console.log(collection.findByName('Bob'));

I like the idea of being able to search deep object properties but the problem is, there is no guarantee that what people put in the model is just a plain / primitive property and not a function or a Class or whatever. Also, the . notation is just sugar. for example, if you did:

model.set('user.name', 'bob');

this may make sense from your data standpoint as you want your json to actually send 'user.name': 'bob' and not create a deep object. I work with some protocols / apis that have all sorts of weird chars.

If there can be a rule to be defined here as to how to serialize the model and to apply a deep obj property searach, i would be very happy to do so.

mootools/mootools-core#2191 was a similar sort of thing which allowed you to do:

obj.get('foo.bar.name'); // or .set

it did not get pulled into mootools-core. it may end up at mootools-more.

it would probably be trivial to extend the selector engine to do that so its like .find('User[name=Bob]') - i will look at it shortly.

@hrasoa
Copy link
Author

hrasoa commented Oct 21, 2012

ok thanks, i'll wait for this.

@hrasoa hrasoa closed this as completed Oct 21, 2012
DimitarChristoff added a commit that referenced this issue Oct 22, 2012
@DimitarChristoff
Copy link
Member

it's not ready yet, though in your case it seems to work. got some issues I need to fix but working on it. expectation for .find('User') being the same as .find('[User]') is not passing yet. will commit more today or tomorrow.

@DimitarChristoff
Copy link
Member

seems to now work - in the branch bug-fix-5 - I won't merge with master until I am certain it's fine and write 1 more test but at a first reading, .find('User') will pass as well as .find('User[name=Bob]'). let me know if it works, see updated collection-demo.js and view collection.html to test

@hrasoa
Copy link
Author

hrasoa commented Oct 27, 2012

Hi, this works great. Thanks !

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

No branches or pull requests

3 participants