React boilerplate

This commit is contained in:
Borys_Levytskyi
2016-11-20 18:59:05 +02:00
parent 6f5f547361
commit de0dfba04f
40 changed files with 391 additions and 197 deletions

View File

@@ -0,0 +1,72 @@
(function(){
"use strict";
var bindr = {};
bindr.bindChildren = function(container, model) {
var elements = container.querySelectorAll('[data-bind]');
Array.prototype.call(elements, function(el){
});
};
bindr.bind = function(element, model, propertyName) {
};
bindr.attachView = function(viewElement, model) {
var elements = viewElement.querySelectorAll('[data-bindr]'),
count = elements.length,
i =0, el;
for(;i<count; i++){
el = elements[i];
this.bindElement(el, model, el.getAttribute('data-bindr'))
}
};
function bindInput(model, intput, propertyName) {
bindTextInput(intput, model, propertyName);
}
function bindCheckBox(element, model, propertyName) {
element.checked = model[propertyName];
element.addEventListener('changed', function (e) {
model[propertyName] = e.target.checked == true;
});
model.observe(propertyName, function (property, value) {
if (window.event && window.event.target == element) {
return;
}
element.checked = value;
});
}
function bindTextInput(input, model, propertyName) {
input.value = model[propertyName];
input.addEventListener('keyup', function (e) {
model[propertyName] = e.target.value;
});
model.observe(propertyName, function (property, value) {
if (window.event && window.event.target == input) {
return;
}
input.value = value;
});
}
function bindHtmlElement(model, el, propertyName) {
model.observe(propertyName, function(propery, value){
el.innerHTML = value;
});
}
window.core.bindr = bindr;
})();

View File

@@ -0,0 +1,57 @@
(function(app, core){
"use strict";
var should = core.should;
function Command(name) {
this.name = name;
this.executionHandlers = [];
}
Command.prototype.execute = function (cmdArgs) {
cmdArgs = cmdArgs || {};
cmdArgs.commandHandled = false;
for(var i=0; i<this.executionHandlers.length; i++) {
this.executionHandlers[i](cmdArgs);
if(cmdArgs.commandHandled === true) {
return;
}
}
};
Command.prototype.subscribe = function (handler) {
this.executionHandlers.push(handler);
// TODO: unsubcribe
};
app.commandHandlers = {};
app.command = function(name, handler) {
var cmd = this.commandHandlers[name];
if(cmd == null) {
cmd = this.commandHandlers[name] = new commandr.Command(name);
}
if(typeof handler == "function") {
cmd.subscribe(handler);
}
if (typeof handler == "object") {
if(typeof handler.execute != "function"){
console.warn('Given handler is an object, but doesn\'t have "execute" function');
return cmd;
}
this.di.resolveProperties(handler);
cmd.subscribe(handler.execute.bind(handler));
}
return cmd;
};
window.commandr = commandr;
})(window.app, window.core);