How to detect what changed in array in Ember.js? -
i'm on ember 2.12.
i have array selecteddays: [object]
in controller passed component. component has array monthdays: [object]
. objects in component need have selected
property set if can found in array controller.
the problem comparing objects costly, based on moment library method (moment1.issame(moment2, 'day')
). right when add selecteddays
array, every object in monthdays
needs compared every object in selecteddays
. can hundreds or thousands of costly comparisons.
the monthdays
have 42 length (6 weeks) selecteddays
can tens or hundreds.
i wonder possible optimize without bruteforce custom sorting/finding optimization.
the simplest solution be: on controller array change, check changed , modify 1 day matches change. far know:
- you can't send action controller component
- array observers don't "know" changed in array (?)
what other options have?
i set ember twiddle there's visible 200-300ms delay every time click day before becomes selected.
when change selecteddays in controller, know object
going add/remove? if can pass newlyaddeddays object component, , in component didupdateattrs
hook, can check newlyaddeddays , set selected
property in component.
{{my-component selecteddays=selecteddays newlyaddeddays=newlyaddeddays }}
initially newlyaddeddays
empty/undefined.
my-component.js file,
import ember 'ember'; export default ember.component.extend({ init() { this._super(...arguments); //prepare selected property using selecteddays controller , monthddays in component. }, //this hook not called first render. //it called whenever change selecteddays/newlyaddeddays. ie, whenever passed property updated didupdateattrs() { if (ember.ispresent(this.get('newlyaddeddays'))) { //look , update selected property. } } });
from options,
you can't send action controller component
don't this. instead, can try service having modified property , component take there.
array observers don't "know" changed in array
yes. array observers don't know changed, have write code taking diff.
Comments
Post a Comment