Skip to content

Commit

Permalink
use view options instead of custom mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
rmurphey committed May 28, 2012
1 parent 0e7e565 commit 5d67e8a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
6 changes: 0 additions & 6 deletions app/views/base.js
Expand Up @@ -10,11 +10,6 @@ define([

initialize : function(config) {
_.bindAll(this, 'bindTo', 'unbind');

if (config) {
_.extend(this, config);
}

this._bindings = [];
this.prepare();
},
Expand Down Expand Up @@ -91,4 +86,3 @@ define([

return View;
});

11 changes: 6 additions & 5 deletions app/views/recentSearches.js
Expand Up @@ -5,26 +5,27 @@ define([
'text!views/templates/recentSearch.html'
], function(_, View, tpl, itemTpl) {
return View.extend({
options : { },
template : tpl,
itemTpl : _.template(itemTpl),

prepare : function() {
if (!_.isFunction(this.currentSearch)) {
if (!_.isFunction(this.options.currentSearch)) {
throw new Error('Recent searches component needs a currentSearch function');
}
this.bindTo(this.searches, 'add remove change', this._update);
this.bindTo(this.options.searches, 'add remove change', this._update);
},

postRender : function() {
this._update();
},

_update : function() {
this.searches.sort();
this.options.searches.sort();

var tpl = this.itemTpl,
currentSearch = this.currentSearch(),
html = this.searches.map(function(item) {
currentSearch = this.options.currentSearch(),
html = this.options.searches.map(function(item) {
var data = item.toJSON();

if (data.term) {
Expand Down
9 changes: 5 additions & 4 deletions app/views/results.js
Expand Up @@ -4,6 +4,7 @@ define([
'text!views/templates/result.html'
], function(View, tpl, itemTpl) {
return View.extend({
options : { },
template : tpl,

elements : [ 'results' ],
Expand All @@ -24,8 +25,8 @@ define([
},

prepare : function() {
this.bindTo(this.searchData, 'add change', this._update);
this.bindTo(this.searchData, 'fetching', function() {
this.bindTo(this.options.searchData, 'add change', this._update);
this.bindTo(this.options.searchData, 'fetching', function() {
this._empty();
this.reset();
});
Expand Down Expand Up @@ -62,12 +63,12 @@ define([
_update : function() {
var tpl = this.itemTpl,
counts = {
all : this.searchData.length,
all : this.options.searchData.length,
video : 0,
image : 0,
twitter : 0
},
html = this.searchData.map(function(item) {
html = this.options.searchData.map(function(item) {
var type = item.get('type'),
data = item.toJSON();
counts[type] += 1;
Expand Down
16 changes: 2 additions & 14 deletions tests/app/views/base.js
Expand Up @@ -17,19 +17,14 @@ define([
});

describe("#initialize", function() {
it("should ingest a config object", function() {
var V = View.extend({ foo : 'bar' });
c = new V();
expect(c.foo).to.be('bar');
});

it("should run the prepare method", function() {
var flag = false;
c = new View({
var V = View.extend({
prepare : function() {
flag = true;
}
});
c = new V();

expect(flag).to.be(true);
});
Expand All @@ -55,13 +50,6 @@ define([
expect(ret).to.be(c);
});

it("should trigger a render event", function() {
var flag;
c.on('render', function() { flag = true; });
c.render();
expect(flag).to.be(true);
});

it("should call the postRender method", function() {
var flag = false,
V = View.extend({
Expand Down
4 changes: 2 additions & 2 deletions tests/app/views/recentSearches.js
Expand Up @@ -43,8 +43,8 @@ define([

it("should update when there is a new search", function() {
expect(el.html()).not.to.contain('baz');
rs.currentSearch = function() { return 'baz'; };
rs.searches.add({ term : 'baz' });
rs.options.currentSearch = function() { return 'baz'; };
searches.add({ term : 'baz' });
expect(el.html()).to.contain('baz');
expect(el.find('.active').html()).to.contain('baz');
});
Expand Down

0 comments on commit 5d67e8a

Please sign in to comment.