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

Match only set attributes #53

Closed
yratanov opened this issue Jan 16, 2015 · 6 comments
Closed

Match only set attributes #53

yratanov opened this issue Jan 16, 2015 · 6 comments

Comments

@yratanov
Copy link
Contributor

Hello!

Do you think we need match only attributes that are not null?

Current code for handleCreate is:

    if (opts.match) {
      var expectedRequest = {};
      var record = this.getStore().createRecord(modelName, match);
      expectedRequest[modelName] = record.serialize();
      httpOptions.data = JSON.stringify(expectedRequest);
    }

I believe it will not work if model is Post(title: 'Some title', body: 'Some body'), handleCreate is match: {title: 'Some title'} (let's say I don't care about body field here), it will match to title: 'Some title', body: null so my request will not be handled.

This will fix the problem:

    if (opts.match) {
      var expectedRequest = {};
      var record = this.getStore().createRecord(modelName, match);
      expectedRequest[modelName] = record.serialize();
      filteredExpectedRequest = {};
      for(field in expectedRequest) { 
           if(expectedRequest[field] != null) {
                filteredExpectedRequest[field] = expectedRequest[field]; 
           }
      }
      httpOptions.data = JSON.stringify(filteredExpectedRequest);
    }
@danielspaniel
Copy link
Collaborator

Hmmm .. that is a good point .. about partial matches. I think that the idea was only to provide exact match, and I never considered partial match because it was kind of difficult. I tried your code above and it did not actually work. If you can prove otherwise send me a test I can run?
Is this a big need you think, for partial match? I was hoping to get away with exact match and call it a day.

@yratanov
Copy link
Contributor Author

Yes, it is a big need. In most of the cases I don't want to set all fields.

I have taken a look into this and it seems that the problem is

hash.data = JSON.stringify(hash.data);

line in ember-data. Mockjax can't match it partially (since it is a string), it means we can't do this: $.mockjax({url: '/test', data: {test: {some: 'test'}}})

Just curious why ember data "stringifies" data object. JQuery can handle it...

@danielspaniel
Copy link
Collaborator

There may be a way around this .. I am going to try an experiment this weekend to see if I can get the partial match working.
Do you think it's beneficial to have two different ways to match like:

handleCreate('person', { partial_match: { name: 'blah' } })
handleCreate('person', { exact_match: { name: 'blah', age: 30 } })

I am thinking that sometimes you might want an exact match sometimes??

@yratanov
Copy link
Contributor Author

@danielspaniel I think if you can do partial_match, we do not need exact match, just match. When I see match: {field: 'value'} I expect that field value must be "value", I don't tell anything else about rest of the fields with this code, IMO.

@danielspaniel
Copy link
Collaborator

Right .. I see what you mean .. just match as many attributes as you have. Ok .. will work on the partial match and let you know when I am done.

@danielspaniel
Copy link
Collaborator

@yratanov, this issue is now fixed in v0.9.5. Ie. you can now do partial matches.
Check it out and let me know if you see any problems though.

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

No branches or pull requests

2 participants