|
@@ -1,12 +1,43 @@
|
|
|
+
|
|
|
+ * @see Model.url
|
|
|
+ */
|
|
|
$(function () {
|
|
|
+ const onChange = (...args) => {
|
|
|
+ console.log('On change', args, TodoItem);
|
|
|
+ };
|
|
|
+
|
|
|
|
|
|
const TodoItem = Backbone.Model.extend({
|
|
|
+ defaults: {
|
|
|
+ description: "Empty todo",
|
|
|
+ status: "incomplete",
|
|
|
+ },
|
|
|
|
|
|
urlRoot: '/server/todos',
|
|
|
});
|
|
|
+
|
|
|
|
|
|
const todoItem = new TodoItem({ id: 1 });
|
|
|
|
|
|
+
|
|
|
+ todoItem.on('change', onChange);
|
|
|
+
|
|
|
+ todoItem.set({ 'description': 'some other description' }, {silent: true});
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ change - when any attribute is modified
|
|
|
+ change:<attr> - when <attr> is modified
|
|
|
+ destroy - when the model is destroyed
|
|
|
+ sync - when the model successfully synced with the server
|
|
|
+ error - when model save or validation fails
|
|
|
+ all - on any event
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+ const defaultDescription = todoItem.get('description');
|
|
|
+
|
|
|
|
|
|
const x = todoItem.fetch({ async: false });
|
|
|
|
|
@@ -17,16 +48,47 @@ $(function () {
|
|
|
|
|
|
todoItem.save();
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
const TodoView = Backbone.View.extend({
|
|
|
+ tagName: 'article',
|
|
|
+ id: 'todo-view',
|
|
|
+ className: 'todo',
|
|
|
+
|
|
|
+
|
|
|
+ template: _.template('<h3><%= description %></h3>'),
|
|
|
+
|
|
|
+ events: {
|
|
|
+
|
|
|
+ 'click h3': 'alertStatus',
|
|
|
+ },
|
|
|
+
|
|
|
+ alertStatus() {
|
|
|
+ alert('Hey, you clicked the H3');
|
|
|
+ },
|
|
|
+
|
|
|
render: function () {
|
|
|
- const html = '<h3>' + this.model.get('description') + '</h3>';
|
|
|
- $(this.el).html(html);
|
|
|
+ const attributes = this.model.toJSON();
|
|
|
+ const html = this.template(attributes);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ this.$el.html(html);
|
|
|
},
|
|
|
});
|
|
|
+
|
|
|
|
|
|
const todoView = new TodoView({ model: todoItem });
|
|
|
+
|
|
|
+
|
|
|
todoView.render();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
$('#app').html(todoView.el);
|
|
|
});
|
|
|
|