From 14a2f3f71400ee9dc078a9fb7e2bf394ebcbd238 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 3 May 2011 15:43:19 -0400 Subject: [PATCH] more javascript updates --- templates/app_helper.js.erb | 12 +++++++++++- templates/collection_view.js.erb | 11 ++++++++--- templates/collection_view.jst.erb | 2 ++ templates/collection_view_spec.js.erb | 7 ++++++- templates/view.js.erb | 13 +++++++++++-- templates/view_spec.js.erb | 4 ++-- 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/templates/app_helper.js.erb b/templates/app_helper.js.erb index 130667c..933fe3e 100644 --- a/templates/app_helper.js.erb +++ b/templates/app_helper.js.erb @@ -2,7 +2,7 @@ _.extend(Backbone.Collection.prototype, { ensureFetched: function(callback) { - if (this.length == 0 || this._alreadyEnsureFetched) { + if (this._alreadyEnsureFetched) { var _this = this; var _refresher = function() { _this.unbind('refresh', _refresher); @@ -17,3 +17,13 @@ _.extend(Backbone.Collection.prototype, { } }); +_.extend(Backbone.View.prototype, { + attributes: function() { + var attrs = {}; + var _this = this; + _.each(this.attributeFields, function(field) { + attrs[field] = _this.$('input[name="' + field + '"]').val(); + }); + return attrs; + } +}); diff --git a/templates/collection_view.js.erb b/templates/collection_view.js.erb index 43e0b47..eef8e4b 100644 --- a/templates/collection_view.js.erb +++ b/templates/collection_view.js.erb @@ -1,9 +1,11 @@ var <%= plural_object_name %>View = Backbone.View.extend({ + events: { + 'click button.new': 'addNew' + }, template: JST['<%= plural_underscore_name %>/list'], - initialize: function(collection) { - _.bindAll(this, 'render', 'addOne', 'addAll'); + initialize: function() { + _.bindAll(this, 'render', 'addOne', 'addAll', 'new'); - this.collection = collection; this.collection.bind('refresh', this.addAll); this.render(); @@ -19,5 +21,8 @@ var <%= plural_object_name %>View = Backbone.View.extend({ }, addAll: function() { this.collection.each(this.addOne); + }, + addNew: function() { + } }); diff --git a/templates/collection_view.jst.erb b/templates/collection_view.jst.erb index 4882ccc..290e79a 100644 --- a/templates/collection_view.jst.erb +++ b/templates/collection_view.jst.erb @@ -1 +1,3 @@
+ + diff --git a/templates/collection_view_spec.js.erb b/templates/collection_view_spec.js.erb index b625ef9..0958b07 100644 --- a/templates/collection_view_spec.js.erb +++ b/templates/collection_view_spec.js.erb @@ -6,9 +6,14 @@ describe('<%= plural_object_name %>View', function() { }); it('should render', function() { - view = new <%= plural_object_name %>View(collection); + view = new <%= plural_object_name %>View({collection: collection}); view.render(); expect($(view.el)).toContain('.list'); + expect($(view.el)).toContain('button.new'); + }); + + it('should add a new model when new is clicked', function() { + }); }); diff --git a/templates/view.js.erb b/templates/view.js.erb index 3e21056..2febe89 100644 --- a/templates/view.js.erb +++ b/templates/view.js.erb @@ -1,12 +1,21 @@ var <%= object_name %>View = Backbone.View.extend({ + events: { + 'click button.save': 'save' + }, + attributeFields: [], template: JST['<%= underscore_name %>s/view'], initialize: function() { - _.bindAll(this, 'render'); + _.bindAll(this, 'render', 'save'); + + this.model.bind('change', this.render); }, render: function() { $(this.el).html(this.template(this.model.toJSON())); - this.$('button').text(this.model.isNew() ? 'Create' : 'Update'); + this.$('button.save').text(this.model.isNew() ? 'Create' : 'Update'); return this; + }, + save: function() { + this.model.save(this.attributes()); } }); diff --git a/templates/view_spec.js.erb b/templates/view_spec.js.erb index 42536af..2b403d8 100644 --- a/templates/view_spec.js.erb +++ b/templates/view_spec.js.erb @@ -10,7 +10,7 @@ describe('<%= object_name %>View', function() { view = new <%= object_name %>View({model: model}); view.render(); - expect(view.$('button')).toHaveText('Create'); + expect(view.$('button.save')).toHaveText('Create'); }); }); @@ -23,7 +23,7 @@ describe('<%= object_name %>View', function() { view = new <%= object_name %>View({model: model}); view.render(); - expect(view.$('button')).toHaveText('Update'); + expect(view.$('button.save')).toHaveText('Update'); }); }); });