Skip to content
This repository was archived by the owner on Oct 10, 2017. It is now read-only.

Commit

Permalink
fixed marionette app
Browse files Browse the repository at this point in the history
  • Loading branch information
Derick Bailey committed Sep 26, 2012
1 parent 0db6b08 commit 33ddf1b
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
BBCloneMail.module("ContactsApp.Contacts", function(Contacts, App, Backbone, Marionette, $, _){
"use strict";

// EXPERIMENTAL
// ------------
//
// Add support for computed fields

function compute(fieldName, fieldList, callback){
var length = fieldList.length;

var fireCallback = function(){
var fields = {};

for (var i = 0; i<length; i++){
var field = fieldList[i];
fields[field] = this.get(field);
}

var value = callback.call(this, fields);
this.set(fieldName, value);

return value;
}

var computedFunc = function(){
var cb = _.bind(fireCallback, this);

for (var i = 0; i<length; i++){
var field = fieldList[i];
this.on("change:" + field, cb);
}

return cb();
}

computedFunc.computedField = true;

return computedFunc;
}

function computeFields(obj){
for(var field in obj){
if (obj[field] && obj[field].computedField){
obj[field].call(obj);
}
}
}

// Entities
// --------

var Contact = Backbone.Model.extend({
initialize: function(){
computeFields(this);
},

fullName: compute("fullName", ["firstName", "lastName"], function(fields){
return fields.lastName + ", " + fields.firstName;
})
});

var ContactCollection = Backbone.Collection.extend({
Expand Down Expand Up @@ -39,7 +92,7 @@ BBCloneMail.module("ContactsApp.Contacts", function(Contacts, App, Backbone, Mar

Contacts.addInitializer(function(){
var controller = new Contacts.Controller();
App.respondTo("contacts:all", controller.getAll, controller);
App.requestResponse.addHandler("contacts:all", controller.getAll, controller);

Contacts.controller = controller;
});
Expand Down
2 changes: 1 addition & 1 deletion public/javascripts/bbclonemail/bbclonemail.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ BBCloneMail = (function(Backbone){
currentApp.start();
};

App.registerCommand("start:app", App.startSubApp, App);
App.commands.addHandler("start:app", App.startSubApp, App);

return App;
})(Backbone);
178 changes: 100 additions & 78 deletions public/javascripts/vendor/marionette.wreqr.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,104 @@
// Marionette.Wreqr v0.0.0
// -----------------------
// A basic Request/Response and Command pattern implementation
// for Backbone.js applications
//
// Copyright 2012 Muted Solutions, LLC. All Rights Reserved

// Request/Response
// ----------------

(function(Marionette){
"use strict";

var handlers = {};

_.extend(Marionette.Application.prototype, {
respondTo: function(name, handler, context){
var config = {
handler: handler,
context: context
};

handlers[name] = config;
},

request: function(name, args){
var config = handlers[name];

if (!config){
throw new Error("Request handler not found for '" + name + "'");
// Marionette.Wreqr, v0.0.0
// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
// Distributed under MIT license
// http://github.com/marionettejs/marionette.wreqr
Backbone.Marionette.Wreqr = (function(Backbone, Marionette, _){
"option strict";
var Wreqr = {};

Wreqr.Handlers = (function(Backbone, _){
"option strict";

var Handlers = function(){
"use strict";
this._handlers = {};
};

Handlers.extend = Backbone.Model.extend;

_.extend(Handlers.prototype, {
addHandler: function(name, handler, context){
var config = {
callback: handler,
context: context
};

this._handlers[name] = config;
},

getHandler: function(name){
var config = this._handlers[name];

if (!config){
throw new Error("Handler not found for '" + name + "'");
}

return function(){
return config.callback.apply(config.context, arguments);
};
},

removeHandler: function(name){
delete this._handlers[name];
},

removeAllHandlers: function(){
this._handlers = {};
}

return config.handler.call(config.context, args);
},

removeRequestHandler: function(name){
delete handlers[name];
},

clearRequestHandlers: function(){
handlers = {};
}
});

})(Backbone.Marionette);

// Command Pattern
// ---------------

(function(Marionette){
"use strict";

var handlers = {};

_.extend(Marionette.Application.prototype, {

registerCommand: function(name, handler, context){
handlers[name] = {
handler: handler,
context: context
};
},

removeCommand: function(name){
delete handlers[name];
},

clearCommands: function(){
handlers = {};
},

execute: function(name, args){
var config = handlers[name];
if (!config){
throw new Error("Handler not found for '" + name + "'");
});

return Handlers;
})(Backbone, _);

Wreqr.Commands = (function(Wreqr){
"option strict";

return Wreqr.Handlers.extend({
execute: function(name, args){
this.getHandler(name)(args);
}

config.handler.call(config.context, args);
});

})(Wreqr);

Wreqr.RequestResponse = (function(Wreqr){
"option strict";

return Wreqr.Handlers.extend({
request: function(name, args){
return this.getHandler(name)(args);
}
});

})(Wreqr);


(function(){
"option strict";

console.log("foo");

if (Backbone && Backbone.Marionette && Backbone.Marionette.Application){
console.log("bar");

var commands = new Wreqr.Commands();
var reqres = new Wreqr.RequestResponse();

_.extend(Backbone.Marionette.Application.prototype, {
commands: commands,
execute: function(name, argObj){
commands.execute(name, argObj);
},

requestResponse: reqres,
request: function(name, argObj){
return reqres.request(name, argObj);
}
});
}
});
})();


return Wreqr;
})(Backbone, Backbone.Marionette, _);

})(Backbone.Marionette);
18 changes: 12 additions & 6 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,43 @@ var categories = [
var contacts = [
{
id: "09vsjk3209svdjh",
name: "Joe Johnson",
firstName: "Joe",
lastName: "Johnson",
email: "[email protected]",
phone: "555-555-5555"
},
{
id: "45890bdhj4590gf",
name: "Jim Jimson",
firstName: "Jim",
lastName: "Jimson",
email: "[email protected]",
phone: "555-555-5556"
},
{
id: "120985h4509vfh4",
name: "Ben Benson",
firstName: "Ben",
lastName: "Benson",
email: "[email protected]",
phone: "555-555-5557"
},
{
id: "9sd9123njkdas90",
name: "Bob Robertson",
firstName: "Bob",
lastName: "Robertson",
email: "[email protected]",
phone: "555-555-5558"
},
{
id: "lj32kjsd09xzcv3",
name: "Amy Amerson",
firstName: "Amy",
lastName: "Amerson",
email: "[email protected]",
phone: "555-555-5559"
},
{
id: "aa23j45hn45n536",
name: "Julie Julerson",
firstName: "Julie",
lastName: "Julerson",
email: "[email protected]",
phone: "555-555-5560"
}
Expand Down
2 changes: 1 addition & 1 deletion spec/javascripts/helpers/marionetteHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ afterEach(function(){

Backbone.Marionette.TemplateCache.clear();

BBCloneMail.clearRequestHandlers();
BBCloneMail.requestResponse.removeAllHandlers();

window.location.hash = "";
});
2 changes: 1 addition & 1 deletion views/templates/contact-item-template.jade
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script#contact-item-template(type = "text/template")
span.name
|<%= name %>
|<%= fullName %>
span.email
|<%= email %>
span.phone
Expand Down

0 comments on commit 33ddf1b

Please sign in to comment.