Skip to content

Instantly share code, notes, and snippets.

@tazsingh
Created July 22, 2011 16:18
Show Gist options
  • Save tazsingh/1099767 to your computer and use it in GitHub Desktop.
Save tazsingh/1099767 to your computer and use it in GitHub Desktop.
Repitition with CoffeeScript Class setup for each generated file due to "bare => false" option as described in https://github.com/rails/rails/issues/1125
(function() {
window.Blogolio = {
Models: {},
Collections: {},
Routers: {},
Views: {}
};
}).call(this);
(function() {
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
Blogolio.Models.User = (function() {
__extends(User, Backbone.Model);
function User() {
User.__super__.constructor.apply(this, arguments);
}
User.prototype.paramRoot = 'user';
User.prototype.defaults = {
name: null,
username: null
};
return User;
})();
Blogolio.Collections.Users = (function() {
__extends(Users, Backbone.Collection);
function Users() {
Users.__super__.constructor.apply(this, arguments);
}
Users.prototype.model = Blogolio.Models.User;
Users.prototype.url = '/users';
return Users;
})();
}).call(this);
(function() {
var _base;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
}, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
(_base = Blogolio.Views).Users || (_base.Users = {});
Blogolio.Views.Users.Edit = (function() {
__extends(Edit, Backbone.View);
function Edit() {
Edit.__super__.constructor.apply(this, arguments);
}
Edit.prototype.events = {
"submit #edit-user": "update"
};
Edit.prototype.update = function(e) {
e.preventDefault();
e.stopPropagation();
return this.options.model.save(null, {
success: __bind(function(model) {
this.options.model = model;
return window.location.hash = "/" + this.options.model.id;
}, this)
});
};
return Edit;
})();
}).call(this);
(function() {
var _base;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
(_base = Blogolio.Views).Users || (_base.Users = {});
Blogolio.Views.Users.Index = (function() {
__extends(Index, Backbone.View);
function Index() {
Index.__super__.constructor.apply(this, arguments);
}
Index.prototype.initialize = function() {
_.bindAll(this, 'addOne', 'addAll', 'render');
return this.options.users.bind('reset', this.addAll);
};
Index.prototype.addAll = function() {
return this.options.users.each(this.addOne);
};
Index.prototype.addOne = function(user) {
var view;
view = new Blogolio.Views.Users.UserView({
model: user
});
return this.$("tbody").append(view.render().el);
};
return Index;
})();
}).call(this);
(function() {
var _base;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
}, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
(_base = Blogolio.Views).Users || (_base.Users = {});
Blogolio.Views.Users.New = (function() {
__extends(New, Backbone.View);
New.prototype.events = {
"submit #new-user": "save"
};
function New(options) {
New.__super__.constructor.call(this, options);
this.options.model = new this.options.collection.model();
this.options.model.bind("change:errors", __bind(function() {
return this.render();
}, this));
}
New.prototype.save = function(e) {
e.preventDefault();
e.stopPropagation();
this.options.model.unset("errors");
return this.options.collection.create(this.options.model.toJSON(), {
success: __bind(function(model) {
this.options.model = model;
return window.location.hash = "/" + this.options.model.id;
}, this),
error: __bind(function(model, jqXHR) {
return this.options.model.set({
errors: $.parseJSON(jqXHR.responseText)
});
}, this)
});
};
return New;
})();
}).call(this);
(function() {
var _base;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
(_base = Blogolio.Views).Users || (_base.Users = {});
Blogolio.Views.Users.Show = (function() {
__extends(Show, Backbone.View);
function Show() {
Show.__super__.constructor.apply(this, arguments);
}
return Show;
})();
}).call(this);
(function() {
var _base;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
(_base = Blogolio.Views).Users || (_base.Users = {});
Blogolio.Views.Users.User = (function() {
__extends(User, Backbone.View);
function User() {
User.__super__.constructor.apply(this, arguments);
}
User.prototype.events = {
"click .destroy": "destroy"
};
User.prototype.tagName = "tr";
User.prototype.destroy = function() {
this.options.model.destroy();
this.remove();
return false;
};
return User;
})();
}).call(this);
(function() {
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
Blogolio.Routers.Users = (function() {
__extends(Users, Backbone.Router);
function Users() {
Users.__super__.constructor.apply(this, arguments);
}
Users.prototype.initialize = function(options) {
this.users = new Blogolio.Collections.Users();
return this.users.reset(options.users);
};
Users.prototype.routes = {
"/new": "newUser",
"/index": "index",
"/:id/edit": "edit",
"/:id": "show",
".*": "index"
};
Users.prototype.newUser = function() {
return this.view = new Blogolio.Views.Users.New({
collection: this.users
});
};
Users.prototype.index = function() {
return this.view = new Blogolio.Views.Users.Index({
users: this.users
});
};
Users.prototype.show = function(id) {
var user;
user = this.users.get(id);
return this.view = new Blogolio.Views.Users.Show({
model: user
});
};
Users.prototype.edit = function(id) {
var user;
user = this.users.get(id);
return this.view = new Blogolio.Views.Users.Edit({
model: user
});
};
return Users;
})();
}).call(this);
(function() {
}).call(this);
#= require_self
#= require_tree ./models
#= require_tree ./views
#= require_tree ./routers
window.Blogolio =
Models: {}
Collections: {}
Routers: {}
Views: {}
class Blogolio.Models.User extends Backbone.Model
paramRoot: 'user'
defaults:
name: null
username: null
class Blogolio.Collections.Users extends Backbone.Collection
model: Blogolio.Models.User
url: '/users'
class Blogolio.Routers.Users extends Backbone.Router
initialize: (options) ->
@users = new Blogolio.Collections.Users()
@users.reset options.users
routes:
"/new": "newUser"
"/index": "index"
"/:id/edit": "edit"
"/:id": "show"
".*": "index"
newUser: ->
@view = new Blogolio.Views.Users.New(collection: @users)
# $("#users").html(@view.render().el)
index: ->
@view = new Blogolio.Views.Users.Index(users: @users)
# $("#users").html(@view.render().el)
show: (id) ->
user = @users.get(id)
@view = new Blogolio.Views.Users.Show(model: user)
# $("#users").html(@view.render().el)
edit: (id) ->
user = @users.get(id)
@view = new Blogolio.Views.Users.Edit(model: user)
# $("#users").html(@view.render().el)
Blogolio.Views.Users ||= {}
class Blogolio.Views.Users.Edit extends Backbone.View
# template: JST["backbone/templates/users/edit"]
events:
"submit #edit-user": "update"
update: (e) ->
e.preventDefault()
e.stopPropagation()
@options.model.save(null,
success:(model) =>
@options.model = model
window.location.hash = "/#{@options.model.id}"
)
# render: ->
# $(this.el).html(this.template(@options.model.toJSON() ))
#
# this.$("form").backboneLink(@options.model)
#
# return this
Blogolio.Views.Users ||= {}
class Blogolio.Views.Users.Index extends Backbone.View
# template: JST["backbone/templates/users/index"]
initialize: () ->
_.bindAll(this, 'addOne', 'addAll', 'render');
@options.users.bind('reset', this.addAll);
addAll: () ->
@options.users.each(this.addOne)
addOne: (user) ->
view = new Blogolio.Views.Users.UserView({model : user})
this.$("tbody").append(view.render().el)
# render: ->
# $(this.el).html(this.template(users: this.options.users.toJSON() ))
# @addAll()
#
# return this
Blogolio.Views.Users ||= {}
class Blogolio.Views.Users.New extends Backbone.View
# template: JST["backbone/templates/users/new"]
events:
"submit #new-user": "save"
constructor: (options) ->
super(options)
@options.model = new @options.collection.model()
@options.model.bind("change:errors", () =>
this.render()
)
save: (e) ->
e.preventDefault()
e.stopPropagation()
@options.model.unset("errors")
@options.collection.create(@options.model.toJSON(),
success: (model) =>
@options.model = model
window.location.hash = "/#{@options.model.id}"
error: (model, jqXHR) =>
@options.model.set({errors: $.parseJSON(jqXHR.responseText)})
)
# render: ->
# $(this.el).html(this.template(@options.model.toJSON() ))
#
# this.$("form").backboneLink(@options.model)
#
# return this
Blogolio.Views.Users ||= {}
class Blogolio.Views.Users.Show extends Backbone.View
# template: JST["backbone/templates/users/show"]
# render: ->
# $(this.el).html(this.template(this.options.model.toJSON() ))
# return this
Blogolio.Views.Users ||= {}
class Blogolio.Views.Users.User extends Backbone.View
# template: JST["backbone/templates/users/user"]
events:
"click .destroy" : "destroy"
tagName: "tr"
destroy: () ->
@options.model.destroy()
this.remove()
return false
# render: ->
# $(this.el).html(this.template(this.options.model.toJSON() ))
# return this
@TrevorBurnham
Copy link

The repetition is unfortunate, but I think the gains from having modularized code outweigh the costs of the repetition. Hopefully future minifiers will be smart enough to pull those helpers out. For now, if you really wanted to squeeze out those few extra bytes, I'd suggest pulling out CoffeeScript's __extends helper and applying it yourself rather than using the extends keyword. That way you can still keep your code modularized.

@tazsingh
Copy link
Author

Thanks for the response. That's actually what I ended up doing as I tweeted about it. For example:

Blogolio.Views.Users.User = Backbone.View.extend {
  #...
}

I'll play with it more and hopefully come up with a solution that keeps Classes but avoids repetition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment