Skip to content

Commit

Permalink
0.5.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Baker committed Oct 5, 2017
1 parent 4d90373 commit b410d99
Show file tree
Hide file tree
Showing 6 changed files with 647 additions and 0 deletions.
81 changes: 81 additions & 0 deletions dist/amd/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*react-view-model@0.5.8#component*/
define([
'require',
'exports',
'module',
'react',
'can-define/map',
'can-util/js/assign',
'./observer',
'./make-enumerable',
'./helpers/autobind-methods',
'can-util/js/dev',
'can-namespace'
], function (require, exports, module) {
var React = require('react');
var DefineMap = require('can-define/map');
var assign = require('can-util/js/assign');
var Observer = require('./observer');
var makeEnumerable = require('./make-enumerable');
var autobindMethods = require('./helpers/autobind-methods');
var dev = require('can-util/js/dev');
var namespace = require('can-namespace');
if (React) {
var Component = function Component() {
React.Component.call(this);
if (this.constructor.ViewModel) {
autobindMethods(this.constructor.ViewModel, true);
if (!makeEnumerable.isEnumerable(this.constructor.ViewModel)) {
makeEnumerable(this.constructor.ViewModel, true);
}
}
this._observer = new Observer();
if (typeof this.shouldComponentUpdate === 'function') {
this._shouldComponentUpdate = this.shouldComponentUpdate;
}
this.shouldComponentUpdate = function () {
return false;
};
};
Component.prototype = Object.create(React.Component.prototype);
assign(Component.prototype, {
constructor: Component,
componentWillReceiveProps: function (nextProps) {
var props = {};
for (var key in nextProps) {
if (!(key in this.props) || nextProps[key] !== this.props[key]) {
props[key] = nextProps[key];
}
}
this.viewModel.assign(props);
},
componentWillMount: function () {
var ViewModel = this.constructor.ViewModel || DefineMap;
this.viewModel = new ViewModel(this.props);
this._observer.startLisening(function () {
if (typeof this._shouldComponentUpdate !== 'function' || this._shouldComponentUpdate()) {
this.forceUpdate();
}
}.bind(this));
},
componentDidMount: function () {
this._observer.stopListening();
},
componentWillUpdate: function () {
this._observer.startLisening();
},
componentDidUpdate: function () {
this._observer.stopListening();
},
componentWillUnmount: function () {
this._observer.stop();
this.viewModel = null;
}
});
module.exports = namespace.ReactViewModelComponent = Component;
} else {
module.exports = namespace.ReactViewModelComponent = function Component() {
throw new Error('You must provide React before can.all.js');
};
}
});
40 changes: 40 additions & 0 deletions dist/amd/helpers/autobind-methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*react-view-model@0.5.8#helpers/autobind-methods*/
define([
'require',
'exports',
'module',
'can-util/js/each',
'can-define/map'
], function (require, exports, module) {
var each = require('can-util/js/each');
var DefineMap = require('can-define/map');
var METHODS_TO_AUTOBIND_KEY = '_methodsToAutobind-react-view-models';
module.exports = function autoBindMethods(ViewModel) {
if (ViewModel[METHODS_TO_AUTOBIND_KEY]) {
return;
}
var setup = ViewModel.prototype.setup;
var methods = getMethods(ViewModel.prototype, {});
Object.defineProperty(ViewModel, METHODS_TO_AUTOBIND_KEY, {
enumerable: false,
value: methods
});
ViewModel.prototype.setup = function setUpWithAutoBind() {
for (var key in methods) {
this[key] = methods[key].bind(this);
}
return setup.apply(this, arguments);
};
};
function getMethods(proto, methods) {
if (proto && proto !== Object.prototype && proto !== DefineMap.prototype) {
each(proto._define.methods, function (property, key) {
if (!(key in methods) && key !== 'constructor') {
methods[key] = property;
}
});
return getMethods(Object.getPrototypeOf(proto), methods);
}
return methods;
}
});
35 changes: 35 additions & 0 deletions dist/amd/make-enumerable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*react-view-model@0.5.8#make-enumerable*/
define([
'require',
'exports',
'module',
'can-util/js/each'
], function (require, exports, module) {
var each = require('can-util/js/each');
module.exports = function makeEnumerable(Type, recursive) {
if (recursive === undefined) {
recursive = true;
}
var setup = Type.prototype.setup;
Type.prototype.setup = function () {
var map = this;
each(this._define.definitions, function (value, prop) {
var parent = Object.getOwnPropertyDescriptor(map.constructor.prototype, prop);
Object.defineProperty(map, prop, {
enumerable: true,
get: parent.get,
set: parent.set
});
if (recursive && value.Type && !isEnumerable(value.Type)) {
makeEnumerable(value.Type, recursive);
}
});
return setup.apply(this, arguments);
};
Type.__isEnumerable = true;
};
function isEnumerable(Type) {
return !!Type.__isEnumerable;
}
module.exports.isEnumerable = isEnumerable;
});
45 changes: 45 additions & 0 deletions dist/amd/observer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*react-view-model@0.5.8#observer*/
define([
'require',
'exports',
'module',
'can-observation',
'can-util/js/assign'
], function (require, exports, module) {
var Observation = require('can-observation');
var assign = require('can-util/js/assign');
function Observer() {
var self = this;
Observation.call(self, null, null, function () {
return self.listener && self.listener();
});
}
Observer.prototype = Object.create(Observation.prototype);
Observer.prototype.constructor = Observer;
assign(Observer.prototype, {
start: function () {
this.value = {};
},
startLisening: function (listener) {
this.listener = listener || this.listener;
this.bound = true;
this.oldObserved = this.newObserved || {};
this.ignore = 0;
this.newObserved = {};
Observation.observationStack.push(this);
},
stopListening: function () {
if (Observation.observationStack[Observation.observationStack.length - 1] !== this) {
var index = Observation.observationStack.indexOf(this);
if (index === -1) {
throw new Error('Async observations stopped out of order.');
}
Observation.observationStack.splice(index, 1);
Observation.observationStack.push(this);
}
Observation.observationStack.pop();
this.updateBindings();
}
});
module.exports = Observer;
});
55 changes: 55 additions & 0 deletions dist/amd/react-view-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*react-view-model@0.5.8#react-view-model*/
define([
'require',
'exports',
'module',
'can-util/js/assign',
'./component',
'can-namespace'
], function (require, exports, module) {
var assign = require('can-util/js/assign');
var Component = require('./component');
var namespace = require('can-namespace');
module.exports = namespace.reactViewModel = function reactViewModel(displayName, ViewModel, render) {
if (arguments.length === 1) {
render = arguments[0];
ViewModel = null;
displayName = null;
}
if (arguments.length === 2) {
render = arguments[1];
if (typeof arguments[0] === 'string') {
displayName = arguments[0];
ViewModel = null;
} else {
ViewModel = arguments[0];
displayName = null;
}
}
if (!displayName) {
displayName = (render.displayName || render.name || 'ReactVMComponent') + 'Wrapper';
}
function App() {
Component.call(this);
}
App.ViewModel = ViewModel;
App.displayName = displayName;
App.prototype = Object.create(Component.prototype);
assign(App.prototype, {
constructor: App,
render: function () {
return render(this.viewModel);
}
});
try {
Object.defineProperty(App, 'name', {
writable: false,
enumerable: false,
configurable: true,
value: displayName
});
} catch (e) {
}
return App;
};
});
Loading

0 comments on commit b410d99

Please sign in to comment.