Add items to model is not working
See original GitHub issueHi,
We currently have an infinity model listings a collection of objects. We display this collection in a template with a button to duplicate selected items. Once “duplicate” button clicked, we want the template to be automatically updated with duplicated items.
Here is the route:
model(params) {
let queryParams = Ember.$.extend({}, params, { perPage: 100, startingPage: 1 });
return this.infinityModel("obj", queryParams);
}
Then in the template, we loop on this model to display the objects in a table, with cheboxes and a button to duplicate checked items:
{{async-button action=(action "duplicate") default="Duplicate" pending="Duplicating..." class="btn btn-success" disableWhen=emptyBulk}}
<table>
<tbody>
{{#each model as |obj|}}
{{obj.name}}
<input type="checkbox" ...>
{{/each}}
</tbody>
</table>
In our controller:
duplicate() {
let url = '/duplicate';
return this.get('ajax').post(url, {
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ids: this.get('objIDs')})
}).then((data) => {
// data.obj is an array with duplicated objects
this.get('model').pushObjects(data.objs);
this.get('notifications').success('Objects duplicated');
}).catch((error) => {
this.get('notifications').error(errorMsg(error));
});
}
With ObjIDs being an array of checked objects’ IDs.
We get the following error: internalModel.getRecord is not a function
We’ve tried it all (at least all we could think about) with:
- this.get(‘store’).pushPayload(data.objs)
- this.get(‘model’).addObjects(data.objs)
- loop on data.objs then pushObject or pushPayload or add Object or create new ember record and push it
Nothing is working, at best with have no error but the model (i.e. the template) is never updated.
Would you have any suggestion on how we should do this?
Thanks a lot, Kind regards
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:16
Top GitHub Comments
I think ember 3.0 broke this, I get an immutability error after upgrading when loading the next page (or manually adding or removing an element from the infinityModel). This might be the cause: https://github.com/emberjs/ember.js/pull/16157/files. You can sidestep the issue by updating the content property on the ArrayProxy directly. For example:
messages.addObject(message)
->messages.content.addObject(message._internalModel)
. This doesn’t help the case of the error being thrown in_doUpdate
, I was considering extending InfinityModel and overridingaddObject
, but I guess I’d have to overridepushObjects
andunshiftObjects
as well. I could just file an issue with ember, I’m unsure if this is something they want to support, but maybe they would be sympathetic since it’s an unexpected regression.#271 was merged. See the README for some updates. Lmk what everyone thinks! Will close this for now.