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

Added the ability to pass in extra params that are (computed) properties #43

Merged
merged 1 commit into from
Jul 18, 2015

Conversation

ashrafhasson
Copy link
Contributor

Hi @hhff, I've added the ability to pass in extra params that can be either static or dynamic. If the extra param is a property on the route, its value will be used for setting the extra param.

I hope this is an acceptable enhancement, I'm currently using this method to dynamically change the extra params I'm passing to the back-end.

@kellyselden
Copy link
Collaborator

What about people who already have an extra param and a route property with the same name, but don't expect this behavior?

@hhff
Copy link
Collaborator

hhff commented Jun 20, 2015

Yeah was thinking about that last night. Only thing I could come up with was that binding could be declared as "infinityRoute.category". Don't love it tho

Any ideas @kellyselden / @ashrafhasson ?

@kellyselden
Copy link
Collaborator

Maybe a third option to infinityModel?

export default Ember.Route.extend(InfinityRoute, {
  aRouteProperty: 'theValue',

  model: function(params) {
    var boundParams = { aBoundProperty: 'aRouteProperty' };
    return this.infinityModel('user', { perPage: 5 }, boundParams);
  }
});

@ashrafhasson
Copy link
Contributor Author

@kellyselden , @hhff yeah gotta admit I overlooked that but also thought it might be acceptable to introduce a breaking change, maybe not though.

@ashrafhasson
Copy link
Contributor Author

@kellyselden I think this is actually a good option, it eliminates the guess work, is clean and doesn't break things. I'll make alterations and submit for review.

@hhff
Copy link
Collaborator

hhff commented Jun 20, 2015

Works for me!

Ember.keys(boundParams).forEach(function(key) {
options[key] = _this.get(boundParams[key]);
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we a) use an ES6 fat arrow function in the forEach so we don't have to bind _this, and b) pull this out into a private function so we don't have to have it in both infinityModel & infinityLoad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hhff sorry I seem to have broken the tests and in my haste forgot to run them. a) is done. b) how? I've tried to privatize and while this works it'll still fail the tests and travis. Appreciate a hint, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hhff apologies, fixed it to call the private function.

@ashrafhasson ashrafhasson force-pushed the dynamic_properties branch 2 times, most recently from d97389e to fb759a6 Compare June 23, 2015 03:50
@hhff
Copy link
Collaborator

hhff commented Jun 23, 2015

Looks great to me! @kellyselden / @truenorth can you take a second look?

@kellyselden
Copy link
Collaborator

LGTM

@hhff
Copy link
Collaborator

hhff commented Jun 23, 2015

@ashrafhasson - could we get some test coverage on this? Once we've got some tests I'm keen to merge!!

@kellyselden
Copy link
Collaborator

+1 test coverage

@ashrafhasson
Copy link
Contributor Author

@hhff / @kellyselden will try to allocate time for adding couple tests over the weekend.

@mike-north
Copy link
Collaborator

I'll take a look at this today

@ashrafhasson
Copy link
Contributor Author

Sorry for the delay in coming back to this, also testing is kinda new to me, here's what I've done pretty much following the existing tests as my example:

test('it uses bound params when loading more data', assert => {

  assert.expect(8);

  var RouteObject = Ember.Route.extend(RouteMixin, {
    model() {
      return this.infinityModel('item', {perPage: 1, startingPage: 2}, {category: 'feature'});
    },

    perPageParam: 'testPerPage',
    pageParam: 'testPage',
    totalPagesParam: 'meta.testTotalPages',
    feature: Ember.computed.alias('test'),
    test: 'new'
  });
  var route = RouteObject.create();

  var dummyStore = {
    find(name, params) {
      assert.equal('new', params.category);
      assert.ok(params.testPage);
      return new Ember.RSVP.Promise(resolve => {
        Ember.run(this, resolve, Ember.Object.create({
          items: [{id: 1, name: 'Test'}],
          pushObjects: Ember.K,
          meta: {
            testTotalPages: 3
          }
        }));
      });
    }
  };

  route.store = dummyStore;

  var model;
  Ember.run(() => {
    route.model().then(result => {
      model = result;
    });
  });
  // The controller needs to be set so _infinityLoad() can call
  // pushObjects()
  var dummyController = Ember.Object.create({
    model
  });
  route.set('controller', dummyController);

  assert.equal(true, route.get('_canLoadMore'));

  // Load more
  Ember.run(() => {
    route._infinityLoad();
  });

  assert.equal(false, route.get('_canLoadMore'));
  assert.equal(3, route.get('_currentPage'));
  assert.ok(model.get('reachedInfinity'), 'Should reach infinity');

});

what I'm not sure of is how to change the route's property and assert that another load uses the newer value of the computed property in this unit test but that might be an integration test scenario?

@ashrafhasson
Copy link
Contributor Author

apologies all, I think I figured it out, does this test make sense:

...

  var RouteObject = Ember.Route.extend(RouteMixin, {
    model() {
      return this.infinityModel('item', {perPage: 1, startingPage: 1}, {category: 'feature'});
    },

    perPageParam: 'testPerPage',
    pageParam: 'testPage',
    totalPagesParam: 'meta.testTotalPages',
    feature: Ember.computed.alias('test'),
    test: 'new'
  });
  var route = RouteObject.create();

  var dummyStore = {
    find(name, params) {
      assert.equal(route.get('test'), params.category);
      assert.ok(params.testPage);
      return new Ember.RSVP.Promise(resolve => {
        Ember.run(this, resolve, Ember.Object.create({
          items: [{id: 1, name: 'Test'}, {id: 2, name: 'New Test'}],
          pushObjects: Ember.K,
          meta: {
            testTotalPages: 3
          }
        }));
      });
    }
  };

  route.store = dummyStore;

...

  // Load more
  Ember.run(() => {
    route._infinityLoad();
  });

  assert.equal(true, route.get('_canLoadMore'), 'can load even more data');
  route.set('test', 'hot');
  // Load even more
  Ember.run(() => {
    route._infinityLoad();
  });
  assert.equal(false, route.get('_canLoadMore'));
...

return this.infinityModel('item', {perPage: 1, startingPage: 1}, {category: 'feature'});
},

perPageParam: 'testPerPage',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these 3 lines are necessary, they are testing the name resolution feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, @kellyselden, assuming you meant 317-319, if I don't define a model, how would I test loading more data with the third option that 'infinityModel' would take to define the dynamic property?
The idea is that the two option hashes to 'infinityModel' are merged before submitting a request and this ensures the second option is passed on to the model. I also needed to set the startingPage to 1 and perPage to 1 so that I can load at least twice observing that the dynamic part got updatede on the second request.

@ashrafhasson
Copy link
Contributor Author

@hhff, @kellyselden the test was updated and I removed the unnecessary tests, do you guys recommend I add something else to the test? Is there anything pending that I need to be aware of?

@hhff
Copy link
Collaborator

hhff commented Jul 18, 2015

Looks good to me @ashrafhasson - can you merge master and squash commits?

@ashrafhasson
Copy link
Contributor Author

@hhff rebased and squashed :)

hhff added a commit that referenced this pull request Jul 18, 2015
Added the ability to pass in extra params that are (computed) properties
@hhff hhff merged commit dc7e4ca into adopted-ember-addons:master Jul 18, 2015
@hhff
Copy link
Collaborator

hhff commented Jul 18, 2015

THE MAN!!! thankyou so much for ur work - i know this PR has been hanging for ages @ashrafhasson

@hhff
Copy link
Collaborator

hhff commented Jul 18, 2015

@kellyselden
Copy link
Collaborator

@ashrafhasson Nice work.

@ashrafhasson
Copy link
Contributor Author

awesome, happy to have contributed and thank you for merging my PR.

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

Successfully merging this pull request may close these issues.

4 participants