QubitProducts/cherrytree

Improve the API of model()

KidkArolis opened this issue · 4 comments

I want to name the models so that the child routes could easily access them via the name, but I can't figure out a simple way to do that, e.g. this is clunky:

model: function () {
  var model1 = new Model();
  var model2 = new Model();
  return when.join(model1.fetch(), model2.fetch()).then(function () {
    return {
      model1: model1,
      model2: model2
    }
  });
}

With the above snippet, child routes can do this.get("model1") and they'll get the model1 since it's been "named" in the context - the return value of model.

Another way of achieving this currently is

model: function () {
  this.model1 = new Model();
  this.model2 = new Model();
  return when.join(model1.fetch(), model2.fetch());
}

I like this less (even though it looks a lot neater), since now we're putting stuff on the route directly, instead of keeping it in route.context - this means we can't distinguish what's a model and what's not, which means we can't clean things up, or do things like passing in the parent context into the child routes, etc.

So, perhaps, something like this could work:

model: function () {
  this.context.model1 = new Model();
  this.context.model2 = new Model();
  return when.join(model1.fetch(), model2.fetch());
}

New suggestion

model: function (context, params) {
  var user = new User(params.id, context.get("account").id);
  context.set("user", user);
  return user.fetch();
}

This has been solved with the context functions in 0.4.0. There's still room for improvement, but at least now there's one consistent way of creating contexts for each route in the model hook.