delorean icon indicating copy to clipboard operation
delorean copied to clipboard

Have scheme work for collections of items

Open tommoor opened this issue 10 years ago • 2 comments

It would be nice if the scheme validation and calculation methods also worked for a collection of items as this is typically how Flux stores are arranged. If we discuss some syntax I can probably take this on... my thinking is another attribute on the stores isCollection: true for example.

tommoor avatar Mar 23 '15 21:03 tommoor

agreed. This bugs me as well. I find myself operating directly on scheme properties, usually when dealing with arrays.

Not sure I follow exactly what you are proposing. Other frameworks, such as Backbone, have solved this by mixing in a library like underscore, and I'm not sure I want to introduce a dep like that. Can you be more specific about how you envision this working?

darcyadams avatar Mar 23 '15 21:03 darcyadams

From an API point of view, i might suggest something like this:

Store is a single 'model' (same as now):

var Artist = Flux.createStore({
  scheme: {
    firstName: {
      default: 'Unknown'
    },
    lastName: {
      default: 'Artist'
    }
  }
});

Store is a collection of 'models':

var Artists = Flux.createStore({
  collection: true,
  scheme: {
    firstName: {
      default: 'Unknown'
    },
    lastName: {
      default: 'Artist'
    }
  }
});

In my own stores of this nature I also have the methods add, remove and update that could work something like:

Artists.add({
  id: 1,
  firstName: 'Elvis',
  lastName: 'Presley'
});

Artists.update({id: 1}, {
  firstName: 'Lisa'
});

Artists.remove({id: 1});

Calling set on this store would throw an error.

Maybe this is overkill? Honestly I'm not sure if this is the direction that you want to take delorean or not, it's definitely moving into the backboney arena but that might be a good a thing as I think there will be a lot of folks moving over and hoping for a little more sugar.

tommoor avatar Mar 24 '15 06:03 tommoor