Permalink
Browse files

specs passing

  • Loading branch information...
1 parent 7c45d6b commit 4f2e543a5dfc6b31e6ff6482fc8284156968b687 Rebecca Miller-Webster committed Jun 14, 2011
Showing with 129 additions and 85 deletions.
  1. +2 −2 SpecRunner.html
  2. +54 −4 spec/backbone.viewhelper.spec.js
  3. +73 −79 src/backbone.viewhelper.js
View
@@ -10,9 +10,9 @@
<!-- include source files here... -->
<script type="text/javascript" src="src/jquery-1.6.min.js"></script>
<script type="text/javascript" src="src/jquery-ui-1.8.13.custom.min.js"></script>
- <script type="text/javascript" src="src/backbone.js"></script>
- <script type="text/javascript" src="src/underscore.js"></script>
<script type="text/javascript" src="src/json2.js"></script>
+ <script type="text/javascript" src="src/underscore.js"></script>
+ <script type="text/javascript" src="src/backbone.js"></script>
<script type="text/javascript" src="src/backbone.viewhelper.js"></script>
<!-- include spec files here... -->
@@ -5,10 +5,60 @@ describe("backbone viewhelper", function() {
});
});
describe("creating one viewhelper", function() {
+
+ describe("css", function() {
+ it("should have prefixed show css", function() { expect(this.helper1.css.show).toEqual('one_show'); });
+ it("should have prefixed edit css", function() { expect(this.helper1.css.edit).toEqual('one_edit'); });
+ it("should have prefixed index css", function() { expect(this.helper1.css.index).toEqual('one_list'); });
+ it("should have prefixed 'new' css", function() { expect(this.helper1.css['new']).toEqual('one_new'); });
+ it("should have prefixed form css", function() { expect(this.helper1.css.form).toEqual('one_form'); });
+ it("should have prefixed destroy css", function() { expect(this.helper1.css.destroy).toEqual('one_delete'); });
+ });
+
+ describe("id", function() {
+ it('should have a prefixed attribute', function() { expect(this.helper1.id.attribute).toEqual('one-id'); });
+ it("should have a prefixed tag id", function() { expect(this.helper1.id.tag(1)).toEqual('one_1'); });
+ });
- it("should have a string url", function() {
- expect(this.helper1.url.index()).toEqual(this.helper1.prefix + 's');
- });
+ describe("urls", function() {
+ it("should have an index url", function() {
+ expect(this.helper1.url('index')).toEqual('ones');
+ });
+ it("should have an edit url", function() {
+ expect(this.helper1.url('edit', 1)).toEqual('ones/1/edit');
+ });
+ it("should have a show url", function() {
+ expect(this.helper1.url('show')).toEqual('ones');
+ });
+ it("should have a new url", function() {
+ expect(this.helper1.url('new')).toEqual('ones/new');
+ });
+ });
+ it("should have the correct routes", function() {
+ routes = this.helper1.getRoutes(["edit", "index", "new"], ["edit", "index", "newContact"]);
+ expect(routes).toEqual({
+ 'ones/:id/edit' : 'edit',
+ 'ones' : 'index',
+ 'ones/new' : 'newContact'
+ });
+ });
+
+ describe("links", function() {
+ it("should create a new link", function() { expect(this.helper1.links['new']).toEqual('new_link'); });
+ it("should create an edit link", function() { expect(this.helper1.links.edit).toEqual('edit_link'); });
+ it("should create a delete link", function() { expect(this.helper1.links.destroy).toEqual('delete_link'); });
+ describe("link events", function() {
+ it('should create one correct link event', function() {
+ expect(this.helper1.getLinkEvents(["click"], ["new"], ["newContact"])).toEqual({ 'click .new_link' : 'newContact'});
+ });
+ it('should create multiple correct link events', function() {
+ expect(this.helper1.getLinkEvents(["click", "click"], ["edit", "destroy"], ["edit", "destroy"])).toEqual({
+ 'click .edit_link' : 'edit',
+ 'click .delete_link' : 'destroy'
+ });
+ })
+ });
+ });
});
describe("creating two viewhelpers", function() {
@@ -19,7 +69,7 @@ describe("backbone viewhelper", function() {
});
it("should have a string url", function() {
- expect(this.helper2.url.index()).toEqual(this.helper2.prefix + 's');
+ expect(this.helper2.url('index')).toEqual('twos');
});
@@ -1,54 +1,66 @@
(function(){
- //TODO: add tests
+ //Backbone.ViewHelper
Backbone.ViewHelper = function(options) {
- this._configure(options);
- this.initialize();
- return this;
- };
+ this.defaults = {
+ prefixDelimiter : '_',
+ prefixExempt : {
+ css : [],
+ url : [],
+ id : [],
+ routes : false
+ },
+ css : {
+ show : 'show',
+ edit : 'edit',
+ index : 'list',
+ 'new' : 'new',
+ form : 'form',
+ destroy : 'delete'
+ },
+ links : ['new', 'edit', 'destroy'],
+ url : {
+ show : '',
+ edit : ':id/edit',
+ index : '',
+ 'new' : 'new'
+ },
+ id : {
+ attribute : '-id',
+ tag : '_'
+ }
+ };
- _.extend(Backbone.ViewHelper.prototype, {
- prefixDelimiter : '_',
- prefixExempt : {
- css : [],
- url : [],
- id : [],
- routes : false
- },
- css : {
- show : 'show',
- edit : 'edit',
- index : 'list',
- 'new' : 'new',
- form : 'form',
- destroy : 'delete'
- },
- links : ['new', 'edit', 'destroy'],
- url : {
- show : '',
- edit : ':id/edit',
- index : '',
- 'new' : 'new'
- },
- id : {
- attribute : '-id',
- tag : '_'
- },
- find : function(id) {
+ this.url = function(val, id, includePrefix) {
+ includePrefix = includePrefix || !this.options.prefixExempt.url[val];
+ var url = this.options.url[val];
+ url = id ? url.replace(':id', id) : url;
+ if(includePrefix) {
+ //we don't want a slash at the end of the index
+ var u = this.options.prefix + 's';
+ if(url != '') u = u + '/' +url;
+ url = u;
+ }
+ return url;
+ };
+
+ this.find = function(id) {
return $('#' + this.id.tag(id));
},
- getId : function(el) {
+ this.getId = function(el) {
return $(el).attr(this.id.attribute);
},
- getRoutes : function(routeVals, eventVals, includePrefix) {
- includePrefix = includePrefix | !this.prefixExempt.routes;
+ this.getRoutes = function(routeVals, eventVals, includePrefix) {
+ includePrefix = includePrefix | !this.options.prefixExempt.routes;
var self = this;
var routes = {};
$.each(routeVals, function(i, val) {
- routes[self.url[val](null,includePrefix)] = eventVals[i];
+ console.log(val);
+ console.log(self.options.url);
+ routes[self.url(val, null,includePrefix)] = eventVals[i];
});
return routes;
},
- getLinkEvents : function(eventNames, linkNames, methodNames) {
+ this.getLinkEvents = function(eventNames, linkNames, methodNames) {
var events = {};
var self = this;
$.each(eventNames, function(i, val) {
@@ -57,71 +69,53 @@
});
return events;
},
- _setAttributes : function(attr, func, exempts) {
+ this._setAttributes = function(attrName, func) {
+ this[attrName] = this[attrName] || {};
+ var attr = this.options[attrName];
+ var exempts = this.options.prefixExempt[attrName];
for(var val in attr) {
if(_.include(exempts, val)) return;
var orig = attr[val];
- attr[val] = func(val, orig);
+ this[attrName][val] = func(val, orig);
}
},
- initialize : function() {
+ this.initialize = function() {
var self = this;
- //add prefix to classes
//css
- this._setAttributes(this.css, function(val, orig) {
- return self.prefix + self.prefixDelimiter + orig;
- }, this.prefixExempt.css);
+ this._setAttributes('css', function(val, orig) {
+ return self.options.prefix + self.options.prefixDelimiter + orig;
+ });
this.css.toClass = function(attr) {
return ("." + attr);
}
//links
- var linkTemp = this.links;
this.links = {};
- _.each(linkTemp, function(val) {
- self.links[val] = self.css[val] + "_link";
+ _.each(this.options.links, function(val) {
+ self.links[val] = self.options.css[val] + "_link";
});
- //make urls functions that take the id
- this._setAttributes(this.url, function(val, orig) {
- return (function(newOrig, prefix, includePrefix) {
- return function(id) {
- var url = id ? newOrig.replace(':id', id) : newOrig;
- if(includePrefix) {
- //we don't want a slash at the end of the index
- var u = prefix + 's';
- if(url != '') u = u + '/' +url;
- url = u;
- }
- return url;
- };
- })(orig, self.prefix, !self.prefixExempt.url[val]);
- }, this.prefixExempt.url);
-
//ids
- this._setAttributes(this.id, function(val, orig) {
- return self.prefix + orig;
- }, this.prefixExempt.id);
+ this._setAttributes('id', function(val, orig) {
+ return self.options.prefix + orig;
+ });
this.id.tag = (function(tagPrefix) {
return function(id) {
return tagPrefix + id;
}
})(this.id.tag);
-
+
return this;
},
- _configure : function(options) {
+ this._configure = function(options) {
options = options || {};
- if (this.options) options = _.extend({}, this.options, options);
- if (options.prefix) this.prefix = options.prefix;
- if (options.prefixDelimiter) this.prefixDelimiter = options.prefixDelimiter;
- if (options.prefixExempt) this.prefixExempt = _.extend({}, this.prefixExempt, options.prefixExempt);
- if (options.css) this.css = _.extend({}, this.css, options.css);
- if (options.links) this.links = options.links;
- if (options.url) this.url = _.extend({}, this.url, options.url);
- if (options.id) this.id = _.extend({}, this.id, options.id);
- this.options = options;
+ this.options = $.extend({}, this.defaults, options);
}
- });
+
+
+ //constructor
+ this._configure(options);
+ this.initialize();
+ };
})();

0 comments on commit 4f2e543

Please sign in to comment.