Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

translated bindings into CS

  • Loading branch information...
commit 53fa47b3b54f49b5d8c91f206269a4ebae9ad4ff 1 parent 673a330
@hunterloftis authored
Showing with 282 additions and 267 deletions.
  1. +77 −0 bindings.coffee
  2. +0 −137 bindings.js
  3. +205 −130 lib/bindings.js
View
77 bindings.coffee
@@ -0,0 +1,77 @@
+do (Backbone) ->
+
+ Backbone.Binding['visible'] = class VisibleBinding extends Backbone.Binding
+ initialize: (@attribute) ->
+ onModelChange: ->
+ val = @viewModel.get(@attribute)
+ if val then return $(@node).show()
+ return $(@node).hide()
+
+ Backbone.Binding['text'] = class TextBinding extends Backbone.Binding
+ initialize: (@attribute) ->
+ onModelChange: ->
+ val = @viewModel.get(@attribute)
+ $(@node).text(val)
+
+ Backbone.Binding['val'] = class ValueBinding extends Backbone.Binding
+ initialize: (@attribute) ->
+ start: ->
+ super
+ $(@node).on('keyup change', @onViewChange)
+ onModelChange: ->
+ val = @viewModel.get(@attribute)
+ $(@node).val(val)
+ onViewChange: ->
+ val = $(@node).val()
+ @viewModel.set(@attribute, val)
+ stop: ->
+ super
+ $(@node).off('keyup change', @onViewChange)
+
+ Backbone.Binding['css'] = class CssBinding extends Backbone.Binding
+ initialize: (@className, @attribute, @truth = true) ->
+ onModelChange: ->
+ val = @viewModel.get(@attribute)
+ if Boolean(@truth) == Boolean(val)
+ return $(@node).addClass(@className)
+ return $(@node).removeClass(@className)
+
+ Backbone.Binding['click'] = class ClickBinding extends Backbone.Binding
+ initialize: ->
+ args = _.toArray(arguments)
+ @callback = args.shift()
+ @args = args
+ start: ->
+ $(@node).on('click', @onViewChange)
+ onViewChange: (event) ->
+ event.preventDefault()
+ callback = @viewModel[@callback] ? @viewModel.get(@callback)
+ callback.apply(@viewModel, @args)
+ stop: ->
+ $(@node).off('click', @onViewChange)
+
+ Backbone.Binding['each'] = class EachBinding extends Backbone.Binding
+ initialize: (@attr) ->
+ @container = $(@node)
+ @itemTemplate = @container.html()
+ @container.html('')
+ start: ->
+ @viewModel.on('change:' + @attr, @onCollectionChange)
+ @onCollectionChange()
+ onCollectionChange: ->
+ self = this
+ @container.html('')
+ @collection = @viewModel.get(@attr)
+ if @collection instanceof Backbone.Collection
+ @collection.off('add remove reset change create sort', @onCollectionChange);
+ @collection.each(@renderItem);
+ @collection.on('add remove reset change create sort', @onCollectionChange);
+ else
+ @renderItem(item) for item in @collection
+ renderItem: (viewModel) =>
+ for node in $(@itemTemplate)
+ @container.append(node)
+ viewModel.bindView(@bindingAttr, node)
+ stop: ->
+ @viewModel.off('change:' + @attr, @onCollectionChange)
+
View
137 bindings.js
@@ -1,137 +0,0 @@
-(function(Backbone) {
-
- // Visible Binding
- var VisibleBinding = Backbone.Binding['visible'] = Backbone.Binding.extend({
- initialize: function(attr) {
- this.attribute = attr;
- },
- onModelChange: function() {
- var val = this.viewModel.get(this.attribute);
- if (val) {
- return $(this.node).show();
- }
- return $(this.node).hide();
- }
- });
-
- // Text Binding
- var TextBinding = Backbone.Binding['text'] = Backbone.Binding.extend({
- initialize: function(attr) {
- this.attribute = attr;
- },
- onModelChange: function() {
- var val = this.viewModel.get(this.attribute);
- $(this.node).text(val);
- }
- });
-
- // Value Binding
- var ValueBinding = Backbone.Binding['val'] = Backbone.Binding.extend({
- initialize: function(attr) {
- this.attribute = attr;
- },
- start: function() {
- Backbone.Binding.prototype.start.apply(this, arguments);
- $(this.node).on('keyup change', this.onViewChange);
- },
- onModelChange: function() {
- var val = this.viewModel.get(this.attribute);
- $(this.node).val(val);
- },
- onViewChange: function() {
- var val = $(this.node).val();
- this.viewModel.set(this.attribute, val);
- },
- stop: function() {
- Backbone.Binding.prototype.stop.apply(this, arguments);
- $(this.node).off('keyup change', this.onViewChange);
- }
- });
-
- // Click Binding
- var ClickBinding = Backbone.Binding['click'] = Backbone.Binding.extend({
- initialize: function() {
- var args = _.toArray(arguments);
- this.callback = args.shift();
- this.args = args;
- },
- start: function() {
- $(this.node).on('click', this.onViewChange);
- },
- onViewChange: function(event) {
- event.preventDefault();
- var callback = this.viewModel[this.callback] || this.viewModel.get(this.callback);
- callback.apply(this.viewModel, this.args);
- },
- stop: function() {
- $(this.node).off('click', this.onViewChange);
- }
- });
-
- // CSS Class Binding
- var CssBinding = Backbone.Binding['css'] = Backbone.Binding.extend({
- initialize: function(className, attr, truth) {
- this.truth = (typeof truth !== 'undefined') ? truth : true;
- this.className = className;
- this.attribute = attr;
- },
- onModelChange: function() {
- var val = this.viewModel.get(this.attribute);
- if (Boolean(this.truth) === Boolean(val)) {
- return $(this.node).addClass(this.className);
- }
- return $(this.node).removeClass(this.className);
- }
- });
-
- // Each binding
- var EachBinding = Backbone.Binding['each'] = Backbone.Binding.extend({
- initialize: function(attr) {
- this.attr = attr;
- // Store the inner html fragment
- var node = $(this.node);
- this.itemTemplate = node.html();
- // Clear the inner DOM
- node.html('');
- },
- start: function() {
- this.viewModel.on('change:' + this.attr, this.onCollectionChange);
- this.onCollectionChange();
- },
- onCollectionChange: function() {
- var self = this;
- var node = $(this.node);
- var newFragment;
- // Clear the inner DOM
- node.html('');
- // Get the currently referenced collection
- this.collection = this.viewModel.get(this.attr);
- if (this.collection instanceof Backbone.Collection) {
- // collection is a Backbone.Collection with an each method
- this.collection.off('add remove reset change create sort', this.onCollectionChange);
- this.collection.each(renderVM);
- this.collection.on('add remove reset change create sort', this.onCollectionChange);
- }
- else {
- // collection is a regular Array
- _.each(this.collection, renderVM);
- }
- function renderVM(viewModel) {
- // Build a new DOM fragment from the inner HTML
- newFragment = $(self.itemTemplate);
- // DOM fragments may have multiple nodes, so append and bind each node separately
- _.each(newFragment, function(innerNode) {
- node.append(innerNode);
- viewModel.bindView(self.bindingAttr, innerNode);
- });
- }
- },
- onModelChange: function() {
-
- },
- stop: function() {
- this.viewModel.off('change:' + this.attr, this.onCollectionChange);
- }
- });
-
-})(Backbone);
View
335 lib/bindings.js
@@ -1,137 +1,212 @@
-(function(Backbone) {
-
- // Visible Binding
- var VisibleBinding = Backbone.Binding['visible'] = Backbone.Binding.extend({
- initialize: function(attr) {
- this.attribute = attr;
- },
- onModelChange: function() {
- var val = this.viewModel.get(this.attribute);
- if (val) {
- return $(this.node).show();
+// Generated by CoffeeScript 1.3.3
+(function() {
+ var __hasProp = {}.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); }; };
+
+ (function(Backbone) {
+ var ClickBinding, CssBinding, EachBinding, TextBinding, ValueBinding, VisibleBinding;
+ Backbone.Binding['visible'] = VisibleBinding = (function(_super) {
+
+ __extends(VisibleBinding, _super);
+
+ function VisibleBinding() {
+ return VisibleBinding.__super__.constructor.apply(this, arguments);
}
- return $(this.node).hide();
- }
- });
-
- // Text Binding
- var TextBinding = Backbone.Binding['text'] = Backbone.Binding.extend({
- initialize: function(attr) {
- this.attribute = attr;
- },
- onModelChange: function() {
- var val = this.viewModel.get(this.attribute);
- $(this.node).text(val);
- }
- });
-
- // Value Binding
- var ValueBinding = Backbone.Binding['val'] = Backbone.Binding.extend({
- initialize: function(attr) {
- this.attribute = attr;
- },
- start: function() {
- Backbone.Binding.prototype.start.apply(this, arguments);
- $(this.node).on('keyup change', this.onViewChange);
- },
- onModelChange: function() {
- var val = this.viewModel.get(this.attribute);
- $(this.node).val(val);
- },
- onViewChange: function() {
- var val = $(this.node).val();
- this.viewModel.set(this.attribute, val);
- },
- stop: function() {
- Backbone.Binding.prototype.stop.apply(this, arguments);
- $(this.node).off('keyup change', this.onViewChange);
- }
- });
-
- // Click Binding
- var ClickBinding = Backbone.Binding['click'] = Backbone.Binding.extend({
- initialize: function() {
- var args = _.toArray(arguments);
- this.callback = args.shift();
- this.args = args;
- },
- start: function() {
- $(this.node).on('click', this.onViewChange);
- },
- onViewChange: function(event) {
- event.preventDefault();
- var callback = this.viewModel[this.callback] || this.viewModel.get(this.callback);
- callback.apply(this.viewModel, this.args);
- },
- stop: function() {
- $(this.node).off('click', this.onViewChange);
- }
- });
-
- // CSS Class Binding
- var CssBinding = Backbone.Binding['css'] = Backbone.Binding.extend({
- initialize: function(className, attr, truth) {
- this.truth = (typeof truth !== 'undefined') ? truth : true;
- this.className = className;
- this.attribute = attr;
- },
- onModelChange: function() {
- var val = this.viewModel.get(this.attribute);
- if (Boolean(this.truth) === Boolean(val)) {
- return $(this.node).addClass(this.className);
+
+ VisibleBinding.prototype.initialize = function(attribute) {
+ this.attribute = attribute;
+ };
+
+ VisibleBinding.prototype.onModelChange = function() {
+ var val;
+ val = this.viewModel.get(this.attribute);
+ if (val) {
+ return $(this.node).show();
+ }
+ return $(this.node).hide();
+ };
+
+ return VisibleBinding;
+
+ })(Backbone.Binding);
+ Backbone.Binding['text'] = TextBinding = (function(_super) {
+
+ __extends(TextBinding, _super);
+
+ function TextBinding() {
+ return TextBinding.__super__.constructor.apply(this, arguments);
}
- return $(this.node).removeClass(this.className);
- }
- });
-
- // Each binding
- var EachBinding = Backbone.Binding['each'] = Backbone.Binding.extend({
- initialize: function(attr) {
- this.attr = attr;
- // Store the inner html fragment
- var node = $(this.node);
- this.itemTemplate = node.html();
- // Clear the inner DOM
- node.html('');
- },
- start: function() {
- this.viewModel.on('change:' + this.attr, this.onCollectionChange);
- this.onCollectionChange();
- },
- onCollectionChange: function() {
- var self = this;
- var node = $(this.node);
- var newFragment;
- // Clear the inner DOM
- node.html('');
- // Get the currently referenced collection
- this.collection = this.viewModel.get(this.attr);
- if (this.collection instanceof Backbone.Collection) {
- // collection is a Backbone.Collection with an each method
- this.collection.off('add remove reset change create sort', this.onCollectionChange);
- this.collection.each(renderVM);
- this.collection.on('add remove reset change create sort', this.onCollectionChange);
+
+ TextBinding.prototype.initialize = function(attribute) {
+ this.attribute = attribute;
+ };
+
+ TextBinding.prototype.onModelChange = function() {
+ var val;
+ val = this.viewModel.get(this.attribute);
+ return $(this.node).text(val);
+ };
+
+ return TextBinding;
+
+ })(Backbone.Binding);
+ Backbone.Binding['val'] = ValueBinding = (function(_super) {
+
+ __extends(ValueBinding, _super);
+
+ function ValueBinding() {
+ return ValueBinding.__super__.constructor.apply(this, arguments);
}
- else {
- // collection is a regular Array
- _.each(this.collection, renderVM);
+
+ ValueBinding.prototype.initialize = function(attribute) {
+ this.attribute = attribute;
+ };
+
+ ValueBinding.prototype.start = function() {
+ ValueBinding.__super__.start.apply(this, arguments);
+ return $(this.node).on('keyup change', this.onViewChange);
+ };
+
+ ValueBinding.prototype.onModelChange = function() {
+ var val;
+ val = this.viewModel.get(this.attribute);
+ return $(this.node).val(val);
+ };
+
+ ValueBinding.prototype.onViewChange = function() {
+ var val;
+ val = $(this.node).val();
+ return this.viewModel.set(this.attribute, val);
+ };
+
+ ValueBinding.prototype.stop = function() {
+ ValueBinding.__super__.stop.apply(this, arguments);
+ return $(this.node).off('keyup change', this.onViewChange);
+ };
+
+ return ValueBinding;
+
+ })(Backbone.Binding);
+ Backbone.Binding['css'] = CssBinding = (function(_super) {
+
+ __extends(CssBinding, _super);
+
+ function CssBinding() {
+ return CssBinding.__super__.constructor.apply(this, arguments);
}
- function renderVM(viewModel) {
- // Build a new DOM fragment from the inner HTML
- newFragment = $(self.itemTemplate);
- // DOM fragments may have multiple nodes, so append and bind each node separately
- _.each(newFragment, function(innerNode) {
- node.append(innerNode);
- viewModel.bindView(self.bindingAttr, innerNode);
- });
+
+ CssBinding.prototype.initialize = function(className, attribute, truth) {
+ this.className = className;
+ this.attribute = attribute;
+ this.truth = truth != null ? truth : true;
+ };
+
+ CssBinding.prototype.onModelChange = function() {
+ var val;
+ val = this.viewModel.get(this.attribute);
+ if (Boolean(this.truth) === Boolean(val)) {
+ return $(this.node).addClass(this.className);
+ }
+ return $(this.node).removeClass(this.className);
+ };
+
+ return CssBinding;
+
+ })(Backbone.Binding);
+ Backbone.Binding['click'] = ClickBinding = (function(_super) {
+
+ __extends(ClickBinding, _super);
+
+ function ClickBinding() {
+ return ClickBinding.__super__.constructor.apply(this, arguments);
+ }
+
+ ClickBinding.prototype.initialize = function() {
+ var args;
+ args = _.toArray(arguments);
+ this.callback = args.shift();
+ return this.args = args;
+ };
+
+ ClickBinding.prototype.start = function() {
+ return $(this.node).on('click', this.onViewChange);
+ };
+
+ ClickBinding.prototype.onViewChange = function(event) {
+ var callback, _ref;
+ event.preventDefault();
+ callback = (_ref = this.viewModel[this.callback]) != null ? _ref : this.viewModel.get(this.callback);
+ return callback.apply(this.viewModel, this.args);
+ };
+
+ ClickBinding.prototype.stop = function() {
+ return $(this.node).off('click', this.onViewChange);
+ };
+
+ return ClickBinding;
+
+ })(Backbone.Binding);
+ return Backbone.Binding['each'] = EachBinding = (function(_super) {
+
+ __extends(EachBinding, _super);
+
+ function EachBinding() {
+ this.renderItem = __bind(this.renderItem, this);
+ return EachBinding.__super__.constructor.apply(this, arguments);
}
- },
- onModelChange: function() {
- },
- stop: function() {
- this.viewModel.off('change:' + this.attr, this.onCollectionChange);
- }
- });
+ EachBinding.prototype.initialize = function(attr) {
+ this.attr = attr;
+ this.container = $(this.node);
+ this.itemTemplate = this.container.html();
+ return this.container.html('');
+ };
+
+ EachBinding.prototype.start = function() {
+ this.viewModel.on('change:' + this.attr, this.onCollectionChange);
+ return this.onCollectionChange();
+ };
+
+ EachBinding.prototype.onCollectionChange = function() {
+ var item, self, _i, _len, _ref, _results;
+ self = this;
+ this.container.html('');
+ this.collection = this.viewModel.get(this.attr);
+ if (this.collection instanceof Backbone.Collection) {
+ this.collection.off('add remove reset change create sort', this.onCollectionChange);
+ this.collection.each(this.renderItem);
+ return this.collection.on('add remove reset change create sort', this.onCollectionChange);
+ } else {
+ _ref = this.collection;
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ item = _ref[_i];
+ _results.push(this.renderItem(item));
+ }
+ return _results;
+ }
+ };
+
+ EachBinding.prototype.renderItem = function(viewModel) {
+ var node, _i, _len, _ref, _results;
+ _ref = $(this.itemTemplate);
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ node = _ref[_i];
+ this.container.append(node);
+ _results.push(viewModel.bindView(this.bindingAttr, node));
+ }
+ return _results;
+ };
+
+ EachBinding.prototype.stop = function() {
+ return this.viewModel.off('change:' + this.attr, this.onCollectionChange);
+ };
+
+ return EachBinding;
+
+ })(Backbone.Binding);
+ })(Backbone);
-})(Backbone);
+}).call(this);
Please sign in to comment.
Something went wrong with that request. Please try again.