everyone. I've got a strange behaviour after update Ember from 2.9.1 to 2.10. After this update, when I navigate List links with link-to helper I've got a full page refresh instead of particle refresh. In Ember 2.9.1 it was a particle refresh. Maybe I've missed something in these updates.
Here is my code for List
app/router.js
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'auto',
rootURL: '/app/'
});
Router.map(function() {
this.route('home', { path: '/' });
this.route('list', { path: '/list/:listId' }, function() {
this.route('lead', { path: 'lead/:leadId', resetNamespace: true });
});
this.route('pageNotFound', { path: '/page-not-found' });
});
controllers/list.js
import Ember from 'ember';
export default Ember.Controller.extend({
queryParams: ['page', 'sortBy', 'direction', 'archived'],
page: 1,
perPage: 50,
sortBy: 'createdAt',
direction: 'asc',
archived: false
});
routes/list.js
import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
import RouteMixin from 'ember-cli-pagination/remote/route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, RouteMixin, {
queryParams: {
page: {refreshModel: true},
perPage: {refreshModel: true},
sortBy: {refreshModel: true},
direction: {refreshModel: true},
archived: {refreshModel: true}
},
intercom: Ember.inject.service('intercom'),
leadLink: 'lead',
model(params)
{
var intercom = this.get('intercom');
var store = this.get('store');
var list;
params.leadLink = this.leadLink;
if (params.listId) {
list = this.store.findRecord('list', params.listId).then(function (list) {
intercom.update({
"Emails": list.get('user.discovered_emails'),
"Limit": list.get('user.max_discovered_emails')
});
return list;
});
}
else {
params.title = 'All leads';
}
return Ember.RSVP.hash({
list: list,
leads: this.store.query('lead', {
filter: this.getFilter(params)
}, { reload: true }).then(function (leads) {
params.total = leads.get('meta.total');
leads.forEach(function (lead) {
var list = store.peekRecord('list', lead.get('listId'));
lead.set('list', list);
return lead;
});
return leads;
}),
lists: this.store.peekAll('list'),
params: params
});
},
// ....
templates/components/sidebar-list-item.hbs
{{#link-to "list" list.id (query-params page=1 archived=false)}}<i class="icon-list-unordered"></i> {{list.name}} <span class="text-muted text-thin">{{#if activeLeadsCount}}({{activeLeadsCount}}){{/if}}</span>{{/link-to}}
Thanks for any help.
I would like to point out issues I found in posted code,
1.Inside list route model hook, you are trying to load list model from store by using peekAll and peekRecord and I haven't found the code to load list model to store.
Without doing findAll('list') if you do peekAll or peekRecord you will get nothing, since peekAll or peekRecord both will return whatever already loaded in store it will not fetch it from server.
So you need to load list model records into store. For this you can do it in beforeModel hook, or you can do it any one of the parent route
beforeModel(transition){
this._super(...arguments);
return this.store.findAll('list');
}
2.For link-to helper if we provide dynamic segment value(in your case list.id) then it will always call beforeModel , model and afterModel hook.
3.Also set {refreshModel: false} if you don't want to opt into full transition
Sorry, guys. I found that it was not full refresh. I have application-loading.hbs with loading circle. And it appears only on app boot, but after fix https://github.com/emberjs/ember.js/pull/14545 it appears any time when I refresh list model.
Anyway thanks for help!
Related
I spent a chunk of time yesterday trying to include filter (reflecting the JSONAPI spec) in the query params of part of an Ember app. With Ember Data it is easy enough to pass a filter array to an endpoint, the problem I have is reflecting that filter array in the query params for a particular route. Note: other, non array, query params are working fine.
TL;DR I have tried various options without success and have a solution that really feels unsatisfactory and not at all DRY. I figure that many others must have tackled this problem and have surely found a better solution. Read on for details of what I have tried so far.
I started with something like this (I initially assumed it would work having read the Ember docs on query params):
Controller:
import Controller from '#ember/controller';
export default Controller.extend({
queryParams: ['sort', 'filter'],
sort: 'id',
init() {
this._super(...arguments);
this.set('filter', []);
},
});
Route:
import Route from '#ember/routing/route';
export default Route.extend({
queryParams: {
filter: {
refreshModel: true
},
sort: {
refreshModel: true
}
},
model(params) {
console.log(JSON.stringify(params)); // filter is always []
return this.get('store').query('contact', params);
}
});
Acceptance Test (this was just a proof of concept test before I started on the more complex stuff):
test('visiting /contacts with query params', async function(assert) {
assert.expect(1);
let done = assert.async();
server.createList('contact', 10);
server.get('/contacts', (schema, request) => {
let params = request.queryParams;
assert.deepEqual(
params,
{
sort: '-id',
"filter[firstname]": 'wibble'
},
'Query parameters are passed in as expected'
);
done();
return schema.contacts.all();
});
await visit('/contacts?filter[firstname]=wibble&sort=-id');
});
No matter how I tweaked the above code, params.filter was always [] in the Route model function.
I have searched around for best-practice on what would seem to be a common use case, but have not found anything recent. sarus' solution here from Nov 2015 works, but means that every possible filter key has to be hardcoded in the controller and route, which seems far from ideal to me. Just imagine doing that for 20 possible filter keys! Using sarus' solution, here is code that works for the above acceptance test but as I say imagine having to hardcode 20+ potential filter keys:
Controller:
import Controller from '#ember/controller';
export default Controller.extend({
queryParams: ['sort',
{ firstnameFilter: 'filter[firstname]' }
],
sort: 'id',
firstnameFilter: null,
init() {
this._super(...arguments);
}
});
Route:
import Route from '#ember/routing/route';
export default Route.extend({
queryParams: {
firstnameFilter: {
refreshModel: true
},
sort: {
refreshModel: true
}
},
model(params) {
if (params.firstnameFilter) {
params.filter = {};
params.filter['firstname'] = params.firstnameFilter;
delete params.firstnameFilter;
}
return this.get('store').query('contact', params);
}
});
I hope there's a better way!
If you don't have the requirement to support dynamic filter fields, #jelhan has provided a really good answer to this question already.
If you do need to support dynamic filter fields read on.
First of all, credit should go to #jelhan who put me on the right track by mentioning the possibility of serializing the application URL with JSON.stringify() and encodeURIComponent() together.
Here's example code with this working...
Controller:
import Controller from '#ember/controller';
export default Controller.extend({
queryParams: ['sort', {
filter: {
type: 'array'
}
}],
sort: 'id',
init() {
this._super(...arguments);
this.set('filter', []);
},
});
Route (no changes required):
import Route from '#ember/routing/route';
export default Route.extend({
queryParams: {
filter: {
refreshModel: true
},
sort: {
refreshModel: true
}
},
model(params) {
return this.get('store').query('contact', params);
}
});
Acceptance Test:
test('visiting /contacts with query params', async function(assert) {
assert.expect(1);
let done = assert.async();
server.createList('contact', 10);
server.get('/contacts', (schema, request) => {
let params = request.queryParams;
assert.deepEqual(
params,
{
sort: '-id',
"filter[firstname]": 'wibble',
"filter[lastname]": 'wobble'
},
'Query parameters are passed in as expected'
);
done();
return schema.contacts.all();
});
// The filter is represented by a Javascript object
let filter = {"firstname":"wibble", "lastname":"wobble"};
// The object is converted to a JSON string and then URI encoded and added to the application URL
await visit('/contacts?sort=-id&filter=' + encodeURIComponent(JSON.stringify(filter)));
});
Great! This test passes. The filter defined in the application URL is passed through to the Route. The Route's model hook makes a JSONAPI request with the filter correctly defined. Yay!
As you can see, there's nothing clever there. All we need to do is set the filter in the correct format in the application URL and the standard Ember Query Params setup will just work with dynamic filter fields.
But how can I update the filter query param via an action or link and see that reflected in the application URL and also make the correct JSONAPI request via the Route model hook. Turns out that's easy too:
Example Action (in controller):
changeFilter() {
let filter = {
firstname: 'Robert',
lastname: 'Jones',
category: 'gnome'
};
// Simply update the query param `filter`.
// Note: although filter is defined as an array, it needs to be set
// as a Javascript object to work
// this.set('filter', filter); - this seems to work but I guess I should use transitionToRoute
this.transitionToRoute('contacts', {queryParams: {filter: filter}});
}
For a link (say you want to apply a special filter), you'll need a controller property to hold the filter, we'll call it otherFilter and can then reference that in the link-to:
Example Controller property (defined in init):
init() {
this._super(...arguments);
this.set('filter', []);
this.set('otherFilter', {occupation:'Baker', category: 'elf'});
}
Example link-to:
{{#link-to 'contacts' (query-params filter=otherFilter)}}Change the filters{{/link-to}}
There you have it!
There is no reason to represent filter values in applications URL the same way as they must be for backend call to be JSON API complaint. Therefore I would not use that format for application URLs.
If you don't have the requirement to support dynamic filter fields, I would hard code all of them to have nice URLs like /contacts?firstname=wibble&sort=-id.
Your code would look like this, if you like to support filtering for firstname and lastname:
// Controller
import Controller from '#ember/controller';
export default Controller.extend({
queryParams: ['sort', 'page', 'firstname', 'lastname'],
sort: 'id',
});
// Route
import Route from '#ember/routing/route';
export default Route.extend({
queryParams: {
firstname: {
refreshModel: true
},
lastname: {
refreshModel: true
}
sort: {
refreshModel: true
}
},
model({ firstname, lastname, sort, page }) {
console.log(JSON.stringify(params)); // filter is always []
return this.get('store').query('contact', {
filter: {
firstname,
lastname
},
sort,
page
});
}
});
If you have to support dynamic filter fields, I would represent the filter object in application URL. For serialization you could use JSON.stringify() and encodeURIComponent() together. The URL would then look like /contacts?filter=%7B%22firstname%22%3A%22wibble%22%7D&sort=-id.
My router.js:
Router.map(function() {
this.route('dashboard', { path: '/' });
this.route('list', { path: '/list/:list_id' }, function() {
this.route('prospect', { path: 'prospect/:prospect_id', resetNamespace: true });
});
});
list.hbs:
<div class="content-wrapper">
<div class="content">
{{prospect-list name=model.name prospects=model.prospects openProspect="openProspect"}}
</div>
</div>
{{outlet}}
prospect-list.hbs:
{{#each prospects as |prospect|}}
{{prospect-item prospect=prospect openedId=openedProspectId openProspect="openProspect"}}
{{/each}}
prospect-item.hbs
<td>{{prospect.firstName}}</td>
<td>{{prospect.list.name}}</td>
components/prospect-list.js
import Ember from 'ember';
export default Ember.Component.extend({
openedProspectId: 0,
actions: {
openProspect(prospect) {
this.set('openedProspectId', prospect.get('id'));
this.sendAction('openProspect', prospect);
}
}
});
components/prospect-list.js
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'tr',
classNameBindings: ['isOpen:success'],
isOpen: Ember.computed('openedId', function() {
return this.get('openedId') == this.get('prospect').id;
}),
click: function(ev) {
var target = $(ev.target);
if(!target.is('input'))
{
this.sendAction('openProspect', this.get('prospect'));
}
}
});
Everything works good when I start application in browser with http://localhost:4200, but when I start from http://localhost:4200/list/27/prospect/88 currently loaded prospect (with id 88) is not highlighted in prospect list, because initial openedProspectId set 0.
How can I set openedProspectId in these case?
I can get these id in routes/prospect.js like:
import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, {
model(params) {
return this.store.findRecord('prospect', params.prospect_id);
},
afterModel(params) {
console.log(params.id);
}
});
but how I can pass it to openedProspectId? Or should I build my application on another way?
There are a few things that could be reworked here. I would start by using the link-to helper instead of sending actions when the prospect is clicked. This will give you a uniform starting point (the route) and allows the user to open the prospect in a new window if they decide to.
The route will naturally set the property model on the controller. You could pass this into the individual prospect-item components as activeProspect. Then within that component just compare prospect.id == activeProspect.id to determine if the row should be highlighted.
It does seem odd to me to have a separate route to highlight a prospect, but I'm unaware of your business requirements. You might consider using queryParams to produce a url like this list/27?prospect=88 and reserve the route for the 'full view' of the prospect.
In our app, there is a Company page, which lists all the companies in paginated way. We are using Ember 1.13 and ember-cli-pagination addon for pagination. The data is coming from Rails API, so we are using remote paginated API scenario. Everything is working fine for now, 10 records on each page, next and previous buttons etc. Only problem is when we add a new record, the record is saved but it doesn't show up on the UI immediately, we have to refresh the page for that. There are other parts of our app where we don't have pagination, so when we add a new record, UI is updated immediately without any refresh.
One issue has been reported in the repo of the addon related to this - https://github.com/mharris717/ember-cli-pagination/issues/89
I tried that but it didn't work. Any suggestions?
EDIT
models/company.js
import DS from 'ember-data';
import Ember from 'ember';
import EmberValidations from 'ember-validations';
import Notable from './notable';
export default Notable.extend(EmberValidations, {
// Attributes
companyName : DS.attr('string'),
companyNotes : DS.attr('string'),
.
.
.
// Associations
owner : DS.belongsTo('user', { inverse: 'companiesOwned' }),
assignedTo : DS.hasMany('user', { inverse: 'companiesAssigned', async: true }),
.
.
.
avatar : DS.belongsTo('attachment', { async: true }),
persons : DS.hasMany('person', { async: true }),
.
.
.
});
routes/company.js
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';
import RouteMixin from 'ember-cli-pagination/remote/route-mixin';
export default Ember.Route.extend(RouteMixin, AuthenticatedRouteMixin, {
model: function(params) {
return Ember.RSVP.hash({
company : this.findPaged('company', params),
users : this.store.findAll('user'),
.
.
.
});
},
setupController: function(controller, models) {
this._super(controller, models);
controller.set('model', models.company);
controller.set('users', models.users);
.
.
.
}
});
controllers/company.js
import Ember from 'ember';
import pagedArray from 'ember-cli-pagination/computed/paged-array';
export default Ember.Controller.extend({
listView : false,
newCompany : false,
creatingCompany : false,
showingCompany : false,
editingCompany : false,
// Pagination
queryParams: ['page', 'perPage'],
pageBinding: 'content.page',
perPageBinding: 'content.perPage',
totalPagesBinding: 'content.totalPages',
page: 1,
perPage: 10,
disableDelete : true,
actions: {
createCompany: function() {
// debugger;
var company = this.get('store').createRecord('company');
this.set('company', company);
this.set('editCompanyPane', true);
this.set('disableDelete', true);
},
// Edit Company
editCompany: function(company) {
this.set('company', company);
this.set('editCompanyPane', true);
this.set('disableDelete', false);
},
closeEditCompany: function() {
this.get('company').rollback();
this.set('editCompanyPane', false);
this.set('disableDelete', true);
},
saveCompany: function(company) {
var _this = this;
company.save().then(function() {
Ember.get(_this, 'flashMessages').success('company.flash.companySaveSucessful');
_this.set('editCompanyPane', false);
}, function() {
Ember.get(_this, 'flashMessages').danger('apiFailure');
});
},
// Delete Company
deleteCompany: function(company) {
var _this = this;
company.destroyRecord().then(function() {
Ember.get(_this, 'flashMessages').success('company.flash.companyDeleteSucessful');
_this.set('editCompanyPane', false);
}, function() {
Ember.get(_this, 'flashMessages').danger('apiFailure');
});
},
}
})
Templates is just a list of ten records per page. Exactly same as Remote pagination API example.
{{#each model as |company|}}
<div class="col-md-3">
{{partial 'companies/modal-view'}}
</div>
{{/each}}
{{ page-numbers content=content }}
Put an observer on your model's content length in your controller:
modelLengthObs: function () {
alert('Model's count has changed');
}.observes('model.content.length')
Is it firing? If it isn't it means you created a record but didn't add it to your controller's model. You can do this manually by doing this in your controller after the creation of the record:
this.get('model.content').pushObject('newRecord');
Your model is associated with a list of companies. Adding a new company like you do isn't gonna refresh your model (you aren't reloading the route), so it isn't gonna refresh your template. Try manually adding the new company to your controller's model's content.
Is it working?
PS : You can try pushing it directly to your model instead of its content.
I had the same problem, this line worked for me:
this.get('target.router').refresh();
I added it to the controller of that hbs:
export default Ember.Controller.extend(
{
actions:
{
deleteSomething: function(someData)
{ //do this
$.get("/deleteSomething.php?some_data=" + someData );
//and refresh
this.get('target.router').refresh();
}
}
});
you can find a reference with code example here.
In my EmberJS application I am displaying a list of Appointments. In an action in the AppointmentController I need to get the appointments owner, but the owner always returns "undefined".
My files:
models/appointment.js
import DS from 'ember-data';
export default DS.Model.extend({
appointmentStatus: DS.attr('number'),
owner: DS.hasMany('person'),
date: DS.attr('Date')
});
models/person.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string')
});
templates/appointmentlist.js
{{#each appointment in controller}}
<div>
{{appointment.date}} <button type="button" {{action 'doIt'}}>Do something!</button>
</div>
{{/each }}
controllers/appointmentlist.js
export default Ember.ArrayController.extend({
itemController: 'appointment'
});
controllers/appointment.js
export default Ember.ObjectController.extend({
actions:{
doIt: function(){
var appointment = this.get('model');
var owner = appointment.get('owner'); //returns undefined
//Do something with owner
}
}
});
Now, I know I can change the owner-property to owner: DS.hasMany('person', {async: true}), and then handle the promise returned from appointment.get('owner');, but that is not what I want.
I have discovered that if I do this {{appointment.owner}} or this {{appointment.owner.name}} in the appointmentlist template, the owner record is fetched from the server. So I guess Ember does not load relationships unless they are used in the template.
I think that the solution to my problem is to use the appointmentlists route to fetch the record in the belongsTo relationship. But I can't figure out how.
Maybe something like this?
routes/appointmentlist.js
export default Ember.Route.extend({
model: function() {
return this.store.find('appointment');
},
afterModel: function(appointments){
//what to do
}
});
EDIT
I did this:
routes/appointmentlist.js
export default Ember.Route.extend({
model: function() {
return this.store.find('appointment');
},
afterModel: function(appointments){
$.each(appointments.content, function(i, appointment){
var owner= appointment.get('owner')
});
}
});
and it works, but I do not like the solution...
You are still asynchronously loading those records, so if you are fast enough you could still get undefined. It'd be better to return a promise from the afterModel hook, or just modify the model hook to do it all.
model: function() {
return this.store.find('appointment').then(function(appointments){
return Ember.RSVP.all(appointments.getEach('owner')).then(function(){
return appointments;
});
});
}
or
model: function() {
return this.store.find('appointment');
},
afterModel: function(model, transition){
return Ember.RSVP.all(model.getEach('owner'));
}
Another way to go is:
export default Ember.Controller.extend({
modelChanged: function(){
this.set('loadingRelations',true);
Ember.RSVP.all(this.get('model').getEach('owner')).then(()=>{
this.set('loadingRelations',false);
});
}.observes('model')
});
This way the transition finishes faster and the relations are loaded afterwards. The loading-state can be observed through loadingRelations.
When there are a lot of relations to load I think this gives a better UX.
You want to load all the assocations in the route, because you want to use Fastboot for search engines and better first time site opened experience.
Holding your assocation loading after primary models are loaded, might not be the best decision.
I am using a syntax to load all assocations in the route:
let store = this.store;
let pagePromise = store.findRecord('page', params.page_id);
let pageItemsPromise = pagePromise.then(function(page) {
return page.get('pageItems');
});
return this.hashPromises({
page: pagePromise,
pageItems: pageItemsPromise
});
And for this.hashPromises I got a mixin:
import Ember from 'ember';
export default Ember.Mixin.create({
hashPromises: function(hash) {
let keys = Object.keys(hash);
return Ember.RSVP.hashSettled(hash).then(function(vals) {
let returnedHash = {};
keys.forEach(function(key) {
returnedHash[key] = vals[key].value;
});
return returnedHash;
});
}
});
I read at
http://emberjs.com/guides/controllers/
the following code:
I have a search box and want to send the value of the search box to the SearchController.
App.ApplicationController = Ember.Controller.extend({ // the initial
value of the `search` property search: '',
actions: {
query: function() {
// the current value of the text field
var query = this.get('search');
this.transitionToRoute('search', { query: query });
} } });
How can i get the query parameter in the SearchController and then show it in search.hbs?
I am working with ember- cli.
The router is
import Ember from 'ember';
var Router = Ember.Router.extend({
location: NENV.locationType
});
Router.map(function() {
this.route('search');
});
export default Router;
I set up a route under routes/search.js
export default Ember.Route.extend({
model : function (params) {
console.debug("hi");
return params;
},
setupController: function(controller,model) {
var query = model.query;
console.debug("query is");
console.debug(query);
}
});
When debugging i get an error:
ember More context objects were passed than there are dynamic segments
Thanks,
David
You need to define your search route to be dynamic, so if you change your route definition to something like this
Router.map(function() {
this.resource('search', {path: '/search/:query});
})
This should work as you are expecting. Let me know if anything.
Cheers!