Browse files

views

  • Loading branch information...
1 parent c52a3d7 commit f8deea56c11929c2e34551efb72fd66387e7ae7b @lancejpollard lancejpollard committed Feb 26, 2012
View
32 Cakefile
@@ -121,28 +121,30 @@ window.Tower.logger = if this["_console"] then _console else console
result += code
compileEach 'client/controller', null, (code) ->
result += code
- compileEach 'dispatch', null, (code) ->
- result += code
- compileEach 'middleware', ((path) -> !!path.match(/(location|route)/)), (code) ->
+ compileEach 'client/view', null, (code) ->
result += code
+ compileEach 'dispatch', null, (code) ->
+ result += code
+ compileEach 'middleware', ((path) -> !!path.match(/(location|route)/)), (code) ->
+ result += code
- # result += fs.readFileSync("./src/tower/middleware/router.coffee", "utf-8").replace(/module\.exports\s*=.*\s*/g, "") + "\n"
- mint.coffee result, bare: false, (error, result) ->
- _console.error error.stack if error
- fs.writeFile "./dist/tower.js", result
- unless error
- #result = obscurify(result)
+ # result += fs.readFileSync("./src/tower/middleware/router.coffee", "utf-8").replace(/module\.exports\s*=.*\s*/g, "") + "\n"
+ mint.coffee result, bare: false, (error, result) ->
+ _console.error error.stack if error
+ fs.writeFile "./dist/tower.js", result
+ unless error
+ #result = obscurify(result)
- mint.uglifyjs result, {}, (error, result) ->
- fs.writeFileSync("./dist/tower.min.js", result)
+ mint.uglifyjs result, {}, (error, result) ->
+ fs.writeFileSync("./dist/tower.min.js", result)
- gzip result, (error, result) ->
+ gzip result, (error, result) ->
- fs.writeFileSync("./dist/tower.min.js.gz", result)
+ fs.writeFileSync("./dist/tower.min.js.gz", result)
- console.log "Minified & Gzipped: #{fs.statSync("./dist/tower.min.js.gz").size}"
+ console.log "Minified & Gzipped: #{fs.statSync("./dist/tower.min.js.gz").size}"
- fs.writeFile "./dist/tower.min.js.gz", mint.uglifyjs(result, {})
+ fs.writeFile "./dist/tower.min.js.gz", mint.uglifyjs(result, {})
task 'build-generic', ->
paths = findit.sync('./src')
View
156 dist/tower.js
@@ -713,6 +713,9 @@
isHash: function(object) {
return this.isObject(object) && !(this.isFunction(object) || this.isArray(object) || _.isDate(object) || _.isRegExp(object));
},
+ isBaseObject: function(object) {
+ return object && object.constructor && object.constructor.name === "Object";
+ },
isArray: Array.isArray || function(object) {
return toString.call(object) === '[object Array]';
},
@@ -1654,7 +1657,7 @@
record = null;
options.limit = 1;
this.find(conditions, options, function(error, records) {
- record = records[0];
+ record = records[0] || null;
if (callback) return callback.call(_this, error, record);
});
return record;
@@ -2402,7 +2405,9 @@
options = data;
data = args.pop();
} else {
- options = args.pop();
+ if (Tower.Support.Object.isBaseObject(args[args.length - 1])) {
+ options = args.pop();
+ }
}
}
if (!opts.data) data = {};
@@ -2412,15 +2417,16 @@
if (!options.hasOwnProperty("instantiate")) options.instantiate = true;
if (opts.ids && args.length > 0) ids = _.flatten(args);
if (ids && ids.length > 0) {
+ ids = _.map(ids, function(idOrRecord) {
+ if (idOrRecord instanceof Tower.Model) {
+ return idOrRecord.get("id");
+ } else {
+ return idOrRecord;
+ }
+ });
criteria.where({
id: {
- $in: _.map(ids, function(idOrRecord) {
- if (idOrRecord instanceof Tower.Model) {
- return idOrRecord.get("id");
- } else {
- return idOrRecord;
- }
- })
+ $in: ids
}
});
}
@@ -2478,6 +2484,11 @@
_find: function(conditions, options, callback) {
if (conditions.id && conditions.id.hasOwnProperty("$in") && conditions.id.$in.length === 1) {
return this.store.findOne(conditions, options, callback);
+ } else if (conditions.id && !conditions.id.hasOwnProperty("$in")) {
+ conditions.id = {
+ $in: Tower.Support.Object.toArray(conditions.id)
+ };
+ return this.store.findOne(conditions, options, callback);
} else {
return this.store.find(conditions, options, callback);
}
@@ -3445,8 +3456,7 @@
return this.fields()[name] = new Tower.Model.Attribute(this, name, options);
},
fields: function() {
- var _base;
- return (_base = Tower.metadataFor(this.name)).fields || (_base.fields = {});
+ return this._fields || (this._fields = {});
}
},
get: function(name) {
@@ -4748,17 +4758,17 @@
};
Builder.prototype.fields = function() {
- var attrName, options;
- options = args.extractOptions;
+ var args, attribute, block, options,
+ _this = this;
+ args = Tower.Support.Array.args(arguments);
+ block = Tower.Support.Array.extractBlock(args);
+ options = Tower.Support.Array.extractOptions(args);
options.as = "fields";
options.label || (options.label = false);
- attrName = args.shift() || attribute.name;
- return template.captureHaml(function() {
- var result;
- result = field(attrName, options(function(_field) {
- return template.hamlConcat(fieldset(block).gsub(/\n$/, ""));
- }));
- return template.hamlConcat(result.gsub(/\n$/, ""));
+ attribute = args.shift() || this.attribute;
+ console.log("FIELDS");
+ return this.field(attribute, options, function(_field) {
+ return _this.fieldset(block);
});
};
@@ -4796,8 +4806,10 @@
};
Builder.prototype.field = function() {
- var args, attributeName, block, defaults, options;
+ var args, attributeName, block, defaults, last, options;
args = Tower.Support.Array.args(arguments);
+ last = args[args.length - 1];
+ if (last === null || last === void 0) args.pop();
block = Tower.Support.Array.extractBlock(args);
options = Tower.Support.Array.extractOptions(args);
attributeName = args.shift() || "attribute.name";
@@ -4817,27 +4829,19 @@
};
Builder.prototype.button = function() {
- var options;
- options = args.extractOptions;
- options.reverseMerge({
- as: "submit"
- });
- options.value = args.shift || "Submit";
- return field(options.value, options, block);
+ var args, block, options;
+ args = Tower.Support.Array.args(arguments);
+ block = Tower.Support.Array.extractBlock(args);
+ options = Tower.Support.Array.extractOptions(args);
+ options.as || (options.as = "submit");
+ options.value = args.shift() || "Submit";
+ if (options.as === "submit") {
+ options["class"] = Tower.View.submitFieldsetClass;
+ }
+ return this.field(options.value, options, block);
};
- Builder.prototype.submit = function() {
- return template.captureHaml(function() {
- var result;
- result = fieldset({
- "class": Tower.View.submitFieldsetClass(function(fields) {
- template.hamlConcat(fields.button.apply(fields, args).gsub(/\n$/, ""));
- if (block) return yield(fields);
- })
- });
- return template.hamlConcat(result.gsub(/\n$/, ""));
- });
- };
+ Builder.prototype.submit = Builder.prototype.button;
Builder.prototype.partial = function(path, options) {
if (options == null) options = {};
@@ -4883,7 +4887,7 @@
if (options == null) options = {};
result = Tower.Support.String.parameterize(this.model.constructor.name);
if (options.parentIndex) result += "-" + options.parentIndex;
- result += "-" + this.attribute;
+ result += "-" + (Tower.Support.String.parameterize(this.attribute));
result += "-" + (options.type || "field");
if (this.index != null) result += "-" + this.index;
return result;
@@ -4908,7 +4912,7 @@
field = this.model.constructor.fields()[this.attribute];
options.as || (options.as = field ? Tower.Support.String.camelize(field.type, true) : "string");
this.inputType = inputType = options.as;
- this.required = field.required === true;
+ this.required = !!(field && field.required === true);
classes = [Tower.View.fieldClass, inputType];
if (!(["submit", "fieldset"].indexOf(inputType) > -1)) {
classes.push(field.required ? Tower.View.requiredClass : Tower.View.optionalClass);
@@ -4930,19 +4934,23 @@
index: this.index,
parentIndex: this.parentIndex
});
- (_base = this.labelHTML)["for"] || (_base["for"] = this.inputHTML.id);
- this.labelHTML["class"] = this.addClass(this.labelHTML["class"], [Tower.View.labelClass]);
- if (this.labelValue !== false) {
- this.labelValue || (this.labelValue = Tower.Support.String.camelize(this.attribute.toString()));
+ if (!(["hidden", "submit"].indexOf(inputType) > -1)) {
+ (_base = this.labelHTML)["for"] || (_base["for"] = this.inputHTML.id);
+ this.labelHTML["class"] = this.addClass(this.labelHTML["class"], [Tower.View.labelClass]);
+ if (this.labelValue !== false) {
+ this.labelValue || (this.labelValue = Tower.Support.String.camelize(this.attribute.toString()));
+ }
+ if (options.hint !== false) {
+ this.errorHTML["class"] = this.addClass(this.errorHTML["class"], [Tower.View.errorClass]);
+ if (Tower.View.includeAria && Tower.View.hintIsPopup) {
+ (_base2 = this.errorHTML).role || (_base2.role = "tooltip");
+ }
+ }
}
this.attributes = this.fieldHTML;
- if (options.hint !== false) {
- this.errorHTML["class"] = this.addClass(this.errorHTML["class"], [Tower.View.errorClass]);
- if (Tower.View.includeAria && Tower.View.hintIsPopup) {
- (_base2 = this.errorHTML).role || (_base2.role = "tooltip");
- }
+ if (inputType !== "submit") {
+ (_base3 = this.inputHTML).name || (_base3.name = this.toParam());
}
- (_base3 = this.inputHTML).name || (_base3.name = this.toParam());
this.value = options.value;
this.dynamic = options.dynamic === true;
this.richInput = options.hasOwnProperty("rich_input") ? !!options.rich_input : Tower.View.richInput;
@@ -5010,6 +5018,12 @@
}, options));
};
+ Field.prototype.submitInput = function(key, options) {
+ return this.tag("input", _.extend({
+ type: "submit"
+ }, options));
+ };
+
Field.prototype.fileInput = function(key, options) {
return this.tag("input", _.extend({
type: "file"
@@ -5252,9 +5266,7 @@
return a(_.extend(options, {
href: path,
title: title
- }), function() {
- return title;
- });
+ }), title.toString());
}
};
@@ -5545,6 +5557,9 @@
return title(browserTitle);
});
},
+ urlFor: function() {
+ return Tower.urlFor.apply(Tower, arguments);
+ },
yields: function(key) {
var ending, value;
value = this[key];
@@ -6015,7 +6030,10 @@
});
},
_index: function(callback) {
- return this.respondWithScoped(callback);
+ var _this = this;
+ return this.findCollection(function(error, collection) {
+ return _this.respondWith(collection, callback);
+ });
},
_new: function(callback) {
var _this = this;
@@ -6028,8 +6046,8 @@
var _this = this;
return this.buildResource(function(error, resource) {
if (!resource) return _this.failure(error, callback);
- return resource.save(function(error, success) {
- return _this.respondWithStatus(success, callback);
+ return resource.save(function(error) {
+ return _this.respondWithStatus(!!!error, callback);
});
});
},
@@ -6048,7 +6066,7 @@
_update: function(callback) {
var _this = this;
return this.findResource(function(error, resource) {
- if (!resource) return _this.failure(error, callback);
+ if (error) return _this.failure(error, callback);
return resource.updateAttributes(_this.params[_this.resourceName], function(error) {
return _this.respondWithStatus(!!!error, callback);
});
@@ -6057,7 +6075,7 @@
_destroy: function(callback) {
var _this = this;
return this.findResource(function(error, resource) {
- if (!resource) return _this.failure(error, callback);
+ if (error) return _this.failure(error, callback);
return resource.destroy(function(error) {
return _this.respondWithStatus(!!!error, callback);
});
@@ -6073,7 +6091,7 @@
respondWithStatus: function(success, callback) {
var failureResponder, options, successResponder;
options = {
- records: this.resource
+ records: this.resource || this.collection
};
if (callback && callback.length > 1) {
successResponder = new Tower.Controller.Responder(this, options);
@@ -6108,6 +6126,16 @@
});
});
},
+ findCollection: function(callback) {
+ var _this = this;
+ return this.scoped(function(error, scope) {
+ if (error) return callback.call(_this, error, null);
+ return scope.all(function(error, collection) {
+ _this[_this.collectionName] = _this.collection = collection;
+ if (callback) return callback.call(_this, error, collection);
+ });
+ });
+ },
findParent: function(callback) {
var association, param, parentClass,
_this = this;
@@ -6129,7 +6157,7 @@
var callbackWithScope,
_this = this;
callbackWithScope = function(error, scope) {
- return callback.call(_this, error, scope.where(_this.criteria().query));
+ return callback.call(_this, error, scope.where(_this.criteria()));
};
if (this.hasParent) {
return this.findParent(function(error, parent) {
@@ -6189,7 +6217,7 @@
Responder.prototype._json = function() {
return this.controller.render({
- json: _.flatten(this.options.records)[0]
+ json: this.options.records
});
};
@@ -6324,7 +6352,7 @@
} else {
options = {};
}
- options.records = args;
+ options.records = args[0];
return Tower.Controller.Responder.respond(this, options, callback);
},
_mimesForAction: function() {
View
2 dist/tower.min.js
1 addition, 1 deletion not shown because the diff is too large. Please use a local Git client to view these changes.
View
4 lib/tower.js
@@ -30,6 +30,8 @@ require('./tower/view');
require('./tower/controller');
+require('./tower/server/controller');
+
require('./tower/dispatch');
require('./tower/middleware');
@@ -52,7 +54,7 @@ Tower.cookieSecret = "tower-cookie-secret";
Tower.render = function(string, options) {
if (options == null) options = {};
- return Shift.render(options.type, string, options);
+ return require("mint").render(options.type, string, options);
};
Tower.domain = "localhost";
View
4 lib/tower/client/controller.js
@@ -3,6 +3,10 @@ require('./controller/elements');
require('./controller/events');
+require('./controller/handlers');
+
Tower.Controller.include(Tower.Controller.Elements);
Tower.Controller.include(Tower.Controller.Events);
+
+Tower.Controller.include(Tower.Controller.Handlers);
View
25 lib/tower/client/controller/elements.js
@@ -18,31 +18,6 @@ Tower.Controller.Elements = {
if (options == null) options = {};
return this.elements = this.extractElements(target, options);
},
- clickHandler: function(name, handler, options) {
- var _this = this;
- return $(this.dispatcher).on(name, function(event) {});
- },
- submitHandler: function(name, handler, options) {
- var _this = this;
- return $(this.dispatcher).on(name, function(event) {
- var action, elements, form, method, params, target;
- target = $(event.target);
- form = target.closest("form");
- action = form.attr("action");
- method = (form.attr("data-method") || form.attr("method")).toUpperCase();
- params = form.serializeParams();
- params.method = method;
- params.action = action;
- elements = _.extend({
- target: target,
- form: form
- }, {});
- return _this._dispatch(handler, {
- elements: elements,
- params: params
- });
- });
- },
invalidForm: function() {
var attribute, element, errors, field, _ref, _results;
element = $("#" + this.resourceName + "-" + this.elementName);
View
28 lib/tower/client/controller/handlers.js
@@ -0,0 +1,28 @@
+
+Tower.Controller.Handlers = {
+ ClassMethods: {
+ submitHandler: function(name, handler, options) {
+ var _this = this;
+ return $(this.dispatcher).on(name, function(event) {
+ var action, elements, form, method, params, target;
+ target = $(event.target);
+ form = target.closest("form");
+ action = form.attr("action");
+ method = (form.attr("data-method") || form.attr("method")).toUpperCase();
+ params = form.serializeParams();
+ params.method = method;
+ params.action = action;
+ elements = _.extend({
+ target: target,
+ form: form
+ }, {});
+ return _this._dispatch(handler, {
+ elements: elements,
+ params: params
+ });
+ });
+ }
+ }
+};
+
+module.exports = Tower.Controller.Handlers;
View
4 lib/tower/controller.js
@@ -13,6 +13,10 @@ Tower.Controller = (function(_super) {
return this._instance || (this._instance = new this);
};
+ Controller.metadata = function() {
+ return this._metadata || (this._metadata = {});
+ };
+
function Controller() {
this.constructor._instance = this;
this.headers = {};
View
2 lib/tower/controller/helpers.js
@@ -13,7 +13,7 @@ Tower.Controller.Helpers = {
var layout;
layout = this.constructor._layout;
if (typeof layout === "function") {
- return layout.apply(this);
+ return layout.call(this);
} else {
return layout;
}
View
4 lib/tower/server/controller.js
@@ -0,0 +1,4 @@
+
+require('./controller/events');
+
+Tower.Controller.include(Tower.Controller.Events);
View
3 lib/tower/view/helpers/renderingHelper.js
@@ -62,6 +62,9 @@ Tower.View.RenderingHelper = {
hasContentFor: function(key) {
return !!(this.hasOwnProperty(key) && this[key] && this[key] !== "");
},
+ has: function(key) {
+ return !!(this.hasOwnProperty(key) && this[key] && this[key] !== "");
+ },
contentFor: function(key, block) {
this[key] = block;
return null;
View
3 src/tower.coffee
@@ -14,6 +14,7 @@ require './tower/store'
require './tower/model'
require './tower/view'
require './tower/controller'
+require './tower/server/controller'
require './tower/dispatch'
require './tower/middleware'
require './tower/command'
@@ -26,7 +27,7 @@ Tower.View.store(new Tower.Store.FileSystem(["app/views"]))
Tower.sessionSecret = "tower-session-secret"
Tower.cookieSecret = "tower-cookie-secret"
Tower.render = (string, options = {}) ->
- Shift.render(options.type, string, options)
+ require("mint").render(options.type, string, options)
Tower.domain = "localhost"
View
2 src/tower/client/controller.coffee
@@ -1,5 +1,7 @@
require './controller/elements'
require './controller/events'
+require './controller/handlers'
Tower.Controller.include Tower.Controller.Elements
Tower.Controller.include Tower.Controller.Events
+Tower.Controller.include Tower.Controller.Handlers
View
18 src/tower/client/controller/elements.coffee
@@ -13,24 +13,6 @@ Tower.Controller.Elements =
processElements: (target, options = {}) ->
@elements = @extractElements(target, options)
- clickHandler: (name, handler, options) ->
- $(@dispatcher).on name, (event) =>
-
- submitHandler: (name, handler, options) ->
- $(@dispatcher).on name, (event) =>
- target = $(event.target)
- form = target.closest("form")
- action = form.attr("action")
- method = (form.attr("data-method") || form.attr("method")).toUpperCase()
- params = form.serializeParams()
-
- params.method = method
- params.action = action
-
- elements = _.extend {target: target, form: form}, {}#, @extractElements(target, options)
-
- @_dispatch handler, elements: elements, params: params
-
invalidForm: ->
element = $("##{@resourceName}-#{@elementName}")
View
18 src/tower/client/controller/handlers.coffee
@@ -0,0 +1,18 @@
+Tower.Controller.Handlers =
+ ClassMethods:
+ submitHandler: (name, handler, options) ->
+ $(@dispatcher).on name, (event) =>
+ target = $(event.target)
+ form = target.closest("form")
+ action = form.attr("action")
+ method = (form.attr("data-method") || form.attr("method")).toUpperCase()
+ params = form.serializeParams()
+
+ params.method = method
+ params.action = action
+
+ elements = _.extend {target: target, form: form}, {}#, @extractElements(target, options)
+
+ @_dispatch handler, elements: elements, params: params
+
+module.exports = Tower.Controller.Handlers
View
3 src/tower/controller.coffee
@@ -4,6 +4,9 @@ class Tower.Controller extends Tower.Class
@instance: ->
@_instance ||= new @
+
+ @metadata: ->
+ @_metadata ||= {}
constructor: ->
@constructor._instance = @
View
2 src/tower/controller/helpers.coffee
@@ -9,6 +9,6 @@ Tower.Controller.Helpers =
layout: ->
layout = @constructor._layout
- if typeof(layout) == "function" then layout.apply(@) else layout
+ if typeof(layout) == "function" then layout.call(@) else layout
module.exports = Tower.Controller.Helpers
View
3 src/tower/server/controller.coffee
@@ -0,0 +1,3 @@
+require './controller/events'
+
+Tower.Controller.include Tower.Controller.Events
View
3 src/tower/view/helpers/renderingHelper.coffee
@@ -53,6 +53,9 @@ Tower.View.RenderingHelper =
hasContentFor: (key) ->
!!(@hasOwnProperty(key) && @[key] && @[key] != "")
+ has: (key) ->
+ !!(@hasOwnProperty(key) && @[key] && @[key] != "")
+
contentFor: (key, block) ->
@[key] = block
null

0 comments on commit f8deea5

Please sign in to comment.