Skip to content

Commit

Permalink
remove private view variables to private scope variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Efremov Alexey committed Feb 19, 2014
1 parent 4b51b91 commit 34452a2
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 60 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ module.exports = function(grunt) {
options:{
configFile: 'karma.conf.js',
runnerPort: 9999,
browsers: ['PhantomJS'],
//browsers: ['PhantomJS'],
browsers: ['Chrome'],
},
dist: {
singleRun: true
Expand Down
93 changes: 52 additions & 41 deletions build/backbone-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ var MixinBackbone;
MixinBackbone = function(Backbone) {
var Region;
MixinBackbone = function(BaseClass) {
var currentView, diViews, removeFlag;
var currentView, diViews, removeFlag, var_bindings, varbindUIElements;
currentView = null;
diViews = {};
removeFlag = false;
varbindUIElements = null;
var_bindings = null;
return BaseClass.extend({
setElement: function() {
BaseClass.prototype.setElement.apply(this, arguments);
Expand Down Expand Up @@ -40,7 +42,7 @@ MixinBackbone = function(Backbone) {
delegateEvents: function(events) {
var _ref;
events || (events = _.result(this, 'events'));
if ((this.__bindUIElements != null) && (events != null)) {
if ((varbindUIElements != null) && (events != null)) {
events = _.reduce(events, ((function(_this) {
return function(memo, v, k) {
var part, result, selector, _i, _len;
Expand All @@ -49,7 +51,7 @@ MixinBackbone = function(Backbone) {
for (_i = 0, _len = result.length; _i < _len; _i++) {
part = result[_i];
if (/@ui\.([^ ,]+)/.exec(part) != null) {
selector = _this.__bindUIElements[RegExp.$1];
selector = varbindUIElements[RegExp.$1];
k = k.replace(part, selector);
}
}
Expand Down Expand Up @@ -201,72 +203,80 @@ MixinBackbone = function(Backbone) {
}
},
unbindRegions: function() {
var k, v, _ref, _results;
if (!this.regions) {
return;
}
return _.each(this.regions, (function(_this) {
return function(v, k) {
_this[k].remove();
return delete _this[k];
};
})(this));
_ref = this.regions;
_results = [];
for (k in _ref) {
v = _ref[k];
this[k].remove();
_results.push(delete this[k]);
}
return _results;
},
bindRegions: function() {
var View, el, k, v, _ref, _results;
if (!this.regions) {
return;
}
return _.each(this.regions, (function(_this) {
return function(v, k) {
var View, el;
if (_.isObject(v)) {
el = _.isString(v.el) ? _this.$el.find(v.el) : el = v.el;
View = v.view;
} else {
el = _this.$el.find(v);
View = Region;
}
if (_this[k] != null) {
return _this[k].setElement(el);
} else {
return _this[k] = new View({
el: el
});
}
};
})(this));
_ref = this.regions;
_results = [];
for (k in _ref) {
v = _ref[k];
if (_.isObject(v)) {
el = _.isString(v.el) ? this.$el.find(v.el) : el = v.el;
View = v.view;
} else {
el = this.$el.find(v);
View = Region;
}
if (this[k] != null) {
_results.push(this[k].setElement(el));
} else {
_results.push(this[k] = new View({
el: el
}));
}
}
return _results;
},
bindUIElements: function() {
var k, v, _results;
if (this.ui == null) {
return;
}
this.unbindUIElements();
this.__bindUIElements = _.extend({}, this.ui);
varbindUIElements = _.extend({}, this.ui);
this.ui = {};
return _.each(this.__bindUIElements, (function(_this) {
return function(v, k, ui) {
return _this.ui[k] = _this.$el.find(v);
};
})(this));
_results = [];
for (k in varbindUIElements) {
v = varbindUIElements[k];
_results.push(this.ui[k] = this.$el.find(v));
}
return _results;
},
unbindUIElements: function() {
if (this.__bindUIElements == null) {
if (varbindUIElements == null) {
return;
}
return this.ui = _.extend({}, this.__bindUIElements);
this.ui = varbindUIElements;
return varbindUIElements = null;
},
bindUIEpoxy: function() {
var rx;
if (!this.bindings) {
return;
}
this.unbindUIEpoxy();
this.__bindings = this.bindings;
var_bindings = this.bindings;
rx = /@ui\.([^ ]+)/;
return this.bindings = _.reduce(this.__bindings, ((function(_this) {
return this.bindings = _.reduce(var_bindings, ((function(_this) {
return function(memo, v, k) {
var key, selector;
if (rx.exec(k) != null) {
selector = _this.__bindUIElements[RegExp.$1];
selector = varbindUIElements[RegExp.$1];
key = k.replace(rx, selector);
memo[key] = v;
} else {
Expand All @@ -277,10 +287,11 @@ MixinBackbone = function(Backbone) {
})(this)), {});
},
unbindUIEpoxy: function() {
if (this.__bindings == null) {
if (var_bindings == null) {
return;
}
return this.bindings = _.extend({}, this.__bindings);
this.bindings = var_bindings;
return var_bindings = null;
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion build/backbone-mixin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 17 additions & 13 deletions dist/backbone-mixin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ MixinBackbone = (Backbone)->
currentView = null
diViews = {}
removeFlag = false
varbindUIElements = null
var_bindings = null

BaseClass.extend

Expand All @@ -26,12 +28,12 @@ MixinBackbone = (Backbone)->

delegateEvents:(events)->
events or (events = _.result(this,'events'))
if @__bindUIElements? and events?
if varbindUIElements? and events?
events = _.reduce events, ((memo, v,k)=>
result = k.match(/@ui\.[^ ,]+/g)
if result? then for part in result
if /@ui\.([^ ,]+)/.exec(part)?
selector = @__bindUIElements[RegExp.$1]
selector = varbindUIElements[RegExp.$1]
k = k.replace part, selector
memo[k] = v
memo
Expand Down Expand Up @@ -138,13 +140,13 @@ MixinBackbone = (Backbone)->

unbindRegions:->
return unless @regions
_.each @regions,(v,k)=>
for k,v of @regions
this[k].remove()
delete this[k]

bindRegions:->
return unless @regions
_.each @regions,(v,k)=>
for k,v of @regions
if _.isObject(v)
el = if _.isString(v.el)
@$el.find(v.el)
Expand All @@ -161,23 +163,24 @@ MixinBackbone = (Backbone)->
bindUIElements:->
return unless @ui?
@unbindUIElements()
@__bindUIElements = _.extend {}, @ui
varbindUIElements = _.extend {}, @ui
@ui = {}
_.each @__bindUIElements,(v,k,ui)=>
for k, v of varbindUIElements
@ui[k] = @$el.find v

unbindUIElements:->
return unless @__bindUIElements?
@ui = _.extend {}, @__bindUIElements
return unless varbindUIElements?
@ui = varbindUIElements
varbindUIElements = null

bindUIEpoxy:->
return unless @bindings
@unbindUIEpoxy()
@__bindings = @bindings
var_bindings = @bindings
rx = /@ui\.([^ ]+)/
@bindings = _.reduce @__bindings, ((memo, v,k)=>
@bindings = _.reduce var_bindings, ((memo, v,k)=>
if rx.exec(k)?
selector = @__bindUIElements[RegExp.$1]
selector = varbindUIElements[RegExp.$1]
key = k.replace rx, selector
memo[key] = v
else
Expand All @@ -186,8 +189,9 @@ MixinBackbone = (Backbone)->
),{}

unbindUIEpoxy:->
return unless @__bindings?
@bindings = _.extend {}, @__bindings
return unless var_bindings?
@bindings = var_bindings
var_bindings = null

Region = MixinBackbone(Backbone.View).extend {}

Expand Down
4 changes: 2 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ module.exports = function(config) {
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
//browsers: ['Chrome'],
browsers: ['PhantomJS'],
browsers: ['Chrome'],
//browsers: ['PhantomJS'],


// If browser does not capture in given timeout [ms], kill it
Expand Down
3 changes: 1 addition & 2 deletions test/checkInstanceFields-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ describe "Check MixinBackbone bindUIElements",->
@view.remove()

it "check ui",->
expect(!!@view.ui).toBeTruthy()
expect(!!@view.__bindUIElements).toBeTruthy()
expect(!!@view.ui).toBeTruthy()
expect(_.size(_.keys(@view.ui))).toEqual(2)
expect(!!@view.ui.hello).toBeTruthy()
expect(!!@view.ui.bye).toBeTruthy()
Expand Down

0 comments on commit 34452a2

Please sign in to comment.