question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add items to model is not working

See original GitHub issue

Hi,

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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:16

github_iconTop GitHub Comments

2reactions
bryanhickersoncommented, Apr 1, 2018

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 overriding addObject, but I guess I’d have to override pushObjects and unshiftObjects 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.

0reactions
snewcomercommented, Apr 15, 2018

#271 was merged. See the README for some updates. Lmk what everyone thinks! Will close this for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP dot net mvc - Add item to list not working, It does nothing
The problem is when I add a recipe at /recipe/create it does not work. I want it to show on the /recipe page....
Read more >
Making queries | Django documentation
Django will complain if you try to assign or add an object of the wrong type. ... Each model has at least one...
Read more >
Manage large lists and libraries - Microsoft Support
Learn how to manage large lists and libraries for SharePoint 2013 and SharePoint 2016 on premise by planning and using key list and...
Read more >
How to add an item to the list if it doesn't exist in C#? - Josip ...
Learn how to add an item to the list only if it doesn't exist in C# by following 3 simple steps.
Read more >
Python's .append(): Add Items to Your Lists in Place
Adding items to a list is a fairly common task in Python, so the language provides a ... Work with .append(); Populate lists...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found