Permalink
Browse files

extracting $.resolved as a plugin

  • Loading branch information...
1 parent 59781d0 commit 657eaf6b08478b6ddbf8e1e4cd862c3629713099 @derickbailey derickbailey committed Sep 27, 2012
View
@@ -17,6 +17,7 @@ module.exports = function(grunt) {
src: [
"public/javascripts/vendor/json2.js",
"public/javascripts/vendor/jquery.js",
+ "public/javascripts/vendor/jquery.resolved.js",
"public/javascripts/vendor/underscore.js",
"public/javascripts/vendor/backbone.js",
"public/javascripts/vendor/backbone.compute.js",
@@ -48,6 +49,7 @@ module.exports = function(grunt) {
jasmine : {
src : [
'public/javascripts/vendor/jquery.js',
+ 'public/javascripts/vendor/jquery.resolved.js',
'public/javascripts/vendor/json2.js',
'public/javascripts/vendor/underscore.js',
'public/javascripts/vendor/backbone.js',
@@ -1,75 +1,45 @@
BBCloneMail.module("MailApp.Inbox", function(Inbox, App, Backbone, Marionette, $, _){
"use strict";
- // Experimetal Method
- // ------------------
- //
- // Deferred Method Combinator
- // http://docfuncmod.posterous.com/deferred-method-combinator
- //
- // To resolve a $.Deferred object in a declarative manner, instead of
- // an imperative manner. Order of arguments is specified as the
- // arg list for the function being resolved, then the arg list for
- // the callback function.
- //
- // Example:
- //
- // ```js
- // {
- // someFunc: resolve("anotherFunc", function(anotherFuncArg, someFuncArg){
- // return anotherFuncArg + someFuncArg;
- // }
- // }
- // ```
- //
- function resolve(funcName, cb){
- return function(){
-
- var self = this,
- argList = arguments;
-
- var promise = this[funcName].apply(this, arguments);
-
- $.when(promise).then(function(){
- var parentArgs = Array.prototype.slice.call(argList);
- var args = Array.prototype.slice.call(arguments);
-
- parentArgs.push(args);
- cb.apply(self, _.flatten(parentArgs));
- });
-
- }
- }
-
// Controller
// ----------
var InboxController = function(mainRegion){
this.mainRegion = mainRegion;
+ $.resolve(this);
};
_.extend(InboxController.prototype, {
- // EXPERIMENTAL: show use of `resolve` function here, to get the
+ // EXPERIMENTAL: show use of `$.resolve` function here, to get the
// email list that needs to be displayed.
- showInbox: resolve("getEmail", function(emailList){
- App.execute("show:mail:list", emailList);
- Backbone.history.navigate("");
- }),
+ showInbox: {
+ resolve: "getEmail",
+ run: function(emailList){
+ App.execute("show:mail:list", emailList);
+ Backbone.history.navigate("");
+ }
+ },
- // EXPERIMENTAL: show use of `resolve` function here, to get the
+ // EXPERIMENTAL: show use of `$.resolve` function here, to get the
// email list and filter it down to the one that is needed.
- showMailById: resolve("getEmail", function(id, emailList){
- var emailItem = emailList.get(id);
- App.execute("show:mail:item", emailItem);
- }),
+ showMailById: {
+ resolve: "getEmail",
+ run: function(id, emailList){
+ var emailItem = emailList.get(id);
+ App.execute("show:mail:item", emailItem);
+ }
+ },
- // EXPERIMENTAL: show use of `resolve` function here, to get the
+ // EXPERIMENTAL: show use of `$.resolve` function here, to get the
// email list by category
- showMailByCategory: resolve("getEmailByCategory", function(categoryName, emailList){
- Backbone.history.navigate("categories/" + categoryName);
- App.execute("show:mail:list", emailList);
- }),
+ showMailByCategory: {
+ resolve: "getEmailByCategory",
+ run: function(categoryName, emailList){
+ Backbone.history.navigate("categories/" + categoryName);
+ App.execute("show:mail:list", emailList);
+ }
+ },
getEmail: function(callback){
return App.request("mail:inbox");
@@ -0,0 +1,68 @@
+// $.resolved
+// ----------
+//
+// Copyright (C) 2012 Muted Solutions, LLC. All Rights Reserved.
+// Distributed under MIT licence
+//
+// Based on Deferred Method Combinator
+// http://docfuncmod.posterous.com/deferred-method-combinator
+//
+// To resolve a $.Deferred (promise) from another method, as a
+// pre-requisite to the primary code for a method being executed
+//
+// Example:
+//
+// ```js
+// MyObj = function(){
+// $.resolve(this);
+// }
+//
+// MyObj.prototype.anotherFunc = function(){
+// var def = $.Deferred();
+// def.resolve("foo");
+// return def.promise();
+// }
+//
+// MyObj.prototype.someFunc = {
+// resolve: "anotherFunc",
+// run: function(anotherFuncArg, someFuncArg){
+// return anotherFuncArg + someFuncArg;
+// }
+// };
+//
+// var obj = new MyObj();
+// obj.someFunc("bar"); // => "foobar"
+// ```
+
+$.resolve = (function($){
+ var Resolve = function(obj){
+ for(var attrName in obj){
+ var attr = obj[attrName];
+ if (attr && attr.resolve && attr.run){
+ obj[attrName] = resolveMethod(attr.resolve, attr.run);
+ }
+ }
+ }
+
+ function resolveMethod(funcName, cb){
+ return function(){
+
+ var self = this,
+ argList = arguments;
+
+ var promise = this[funcName].apply(this, arguments);
+
+ $.when(promise).then(function(){
+ var parentArgs = Array.prototype.slice.call(argList);
+ var args = Array.prototype.slice.call(arguments);
+
+ parentArgs.push(args);
+ var flatArgs = $.map(parentArgs, function(arg){ return arg; });
+ cb.apply(self, flatArgs);
+ });
+
+ }
+ }
+
+ return Resolve;
+})($);
View
@@ -3,6 +3,7 @@
// load all scripts individually for development purposes
script(src="/javascripts/vendor/json2.js")
script(src="/javascripts/vendor/jquery.js")
+ script(src="/javascripts/vendor/jquery.resolved.js")
script(src="/javascripts/vendor/underscore.js")
script(src="/javascripts/vendor/backbone.js")
script(src="/javascripts/vendor/backbone.compute.js")

0 comments on commit 657eaf6

Please sign in to comment.