12. Now can be used as
me.set({someAttr:’Today!’});
me.save();
… but we can handle saving too …
.save({success:.., error:..});
… and tie it to some view …
SomeEventView.model
13. Actual model definition
Weekendee.Models.Event = Backbone.Model.extend({
defaults: {
},
url: function () {
if (this.id === undefined) this.id = '';
return base_url + 'event/' + this.id;
},
validate: function (attrs) {
if (attrs.text != null && $.trim(attrs.text) == '') {
return t('event.error_empty');
}
}
});
14. Kohana controller in back
public function action_index() {
$requestMethod = $this->request->method();
switch ($requestMethod) {
case 'GET':
$eventId = $this->request->param('id');
$this->_read();
break;
case 'POST':
$eventId = $this->request->param('id');
if (!$eventId) {
$this->_create();
}
else if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$httpMethodOverride = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
if ($httpMethodOverride == "PUT") {
$this->_update();
}
else if ($httpMethodOverride == "DELETE") {
$this->_delete();
}
}
break;
default:
$this->_read();
}
}
30. Problems
Event propagation - loss of bind context
var me = this;
var comment = new Weekendee.Models.EventComment(commentModel);
me.collection.add(comment);
comment.bind('destroy', me.removeComment, me);
Hard to debug if you’re being dumb – model is
not printable
userModel.set(‘name’,’Dr. Who?’);
32. Problems
(because of) big variety of connectivity
combinations:
X Models (update/get updated by)
Y Views with
Z Collections
And thus.. How to bind stuff correctly?
Manage collection of views?