Skip to content

Commit

Permalink
0.5.8
Browse files Browse the repository at this point in the history
  • Loading branch information
chasenlehara committed Sep 29, 2017
1 parent 69b7498 commit b42b82f
Show file tree
Hide file tree
Showing 5 changed files with 557 additions and 0 deletions.
76 changes: 76 additions & 0 deletions dist/amd/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*react-view-model@0.5.7#component*/
define([
'require',
'exports',
'module',
'react',
'can-define/map',
'can-util/js/assign',
'./observer',
'./make-enumerable',
'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 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 && !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.set(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');
};
}
});
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.7#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.7#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.7#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 b42b82f

Please sign in to comment.