Update model on view?


#1

I’m using Ember Data beta 8 with Ember 1.5.1. I have a single model defined (Message):

MessagingApp.Message = DS.Model.extend({
  subject: DS.attr("string"),
  body: DS.attr("string"),
  priority: DS.attr("number"),
  fromId: DS.attr("string"),
  ringtone: DS.attr("string"),
  read: DS.attr("boolean")
});

I have several routes defined, a message list and a route to view a message:

Messaging.Router.map(function() {
  this.resource("messages", {
    path: "/"
  }, function() {
    this.route("view", {
      path: "/:message_id"
    });
  });
});

I also have a route MessagesViewRoute defined with the following content:

Messaging.MessagesViewRoute = Ember.Route.extend({
  model: function(params) {
    var message;
    message = this.store.find("message", params.message_id);
    if (!message.get("read")) {
      message.set("read", true);
      message.save();
    }
    return message;
  },
  setupController: function(controller, message) {
    controller.set("model", message);
  }
});

This appears to work fine when I transition to the view route from the index, but if I go straight to the view route I get a blank screen with the following unclear error logged to the console:

"Error while loading route: Ember.Error@http://192.168.7.8/sb1/js/vendor.js:17261:1
Ember.assert@http://192.168.7.8/sb1/js/vendor.js:16424:5
Ember.ObjectProxy<.setUnknownProperty@http://192.168.7.8/sb1/js/vendor.js:30293:5
set@http://192.168.7.8/sb1/js/vendor.js:19393:7
Ember.Observable<.set@http://192.168.7.8/sb1/js/vendor.js:28850:5
Messaging.MessagesViewRoute<.model@http://192.168.7.8/sb1/js/main.js:151:7
superWrapper@http://192.168.7.8/sb1/js/vendor.js:17643:5
Ember.Route<.deserialize@http://192.168.7.8/sb1/js/vendor.js:52921:7
HandlerInfo.prototype.runSharedModelHook/<@http://192.168.7.8/sb1/js/vendor.js:49323:11
Router.prototype.async/<@http://192.168.7.8/sb1/js/vendor.js:49815:11
invokeResolver@http://192.168.7.8/sb1/js/vendor.js:25997:9
Promise@http://192.168.7.8/sb1/js/vendor.js:25983:9
Router.prototype.async@http://192.168.7.8/sb1/js/vendor.js:49816:1
HandlerInfo.prototype.runSharedModelHook@http://192.168.7.8/sb1/js/vendor.js:49324:1
UnresolvedHandlerInfoByParam.prototype.getModel@http://192.168.7.8/sb1/js/vendor.js:49409:7
bind/<@http://192.168.7.8/sb1/js/vendor.js:50918:9
invokeCallback@http://192.168.7.8/sb1/js/vendor.js:26364:11
publish@http://192.168.7.8/sb1/js/vendor.js:26034:9
publishFulfillment@http://192.168.7.8/sb1/js/vendor.js:26454:7
@http://192.168.7.8/sb1/js/vendor.js:34731:7
DeferredActionQueues.prototype.flush@http://192.168.7.8/sb1/js/vendor.js:22478:17
Backburner.prototype.end@http://192.168.7.8/sb1/js/vendor.js:22566:11
Backburner.prototype.run@http://192.168.7.8/sb1/js/vendor.js:22605:13
Ember.run@http://192.168.7.8/sb1/js/vendor.js:23015:5
runInitialize@http://192.168.7.8/sb1/js/vendor.js:57254:9
jQuery.Callbacks/fire@http://192.168.7.8/sb1/js/vendor.js:9755:1
jQuery.Callbacks/self.fireWith@http://192.168.7.8/sb1/js/vendor.js:9867:7
.ready@http://192.168.7.8/sb1/js/vendor.js:10073:3
completed@http://192.168.7.8/sb1/js/vendor.js:10088:2

The aim is to update the model when you view it in order to mark the message as read. I believe that the view route would be the best place to do this, but I may be missing out on a core convention.