Skip to content

Instantly share code, notes, and snippets.

@leaysgur
Created September 20, 2016 13:56
Show Gist options
  • Save leaysgur/55ef355827f2b045352b23c91e191d9e to your computer and use it in GitHub Desktop.
Save leaysgur/55ef355827f2b045352b23c91e191d9e to your computer and use it in GitHub Desktop.
More better mobx way?
const Model = require('Model');
class Store {
constructor() {
extendObservable(this, {
items: asFlat([]), // wanna make this Array<Model>
})
}
fooFunc(someData) {
this.items.push(new Model(someData)); // but I don't want to write `new`
}
barFunc(someDataArr) {
this.items.replace(someDataArr.map((data) => { return new Model(data); })); // so many times..
}
}
@leaysgur
Copy link
Author

@leaysgur
Copy link
Author

my workaround.

constructor() {
  // ...

  intercept(this.items, __toModelInterceptor);

  function __toModelInterceptor(change) {
    change.added = change.added.map((item) => {
      return new Model(item);
    });
    return change;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment