Skip to content

Commit

Permalink
concat vjs
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghai committed Jul 21, 2015
1 parent 25c175c commit 485f957
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
37 changes: 31 additions & 6 deletions components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ define([
events: {
'submit': 'onSubmit',
'change input[type="text"]': 'onChange',
'change input[type="file"]': 'onFileChange',
'change input[type="checkbox"]': 'onCheckboxChange',
'change textarea': 'onChange',
'change select': 'onChange',
},
Expand All @@ -38,6 +40,28 @@ define([
target.name && this.model.set(target.name, target.value);
},

onFileChange: function(e) {
var target = e.target;
target.name && this.model.set(target.name, target.files[0]);
},

onCheckboxChange: function(e) {
var target = e.target;
if (!target.name) return;

var checked = target.checked;
var data = this.model[target.name];
if (!data) {
data = [];
}
if (checked) {
data.push(target.value);
} else {
data.splice(data.indexOf(target.value), 1);
}
this.model.set(target.name, data);
},

onValidate: function(data) {
for (var k in data) {
this.$('.' + k)
Expand All @@ -57,13 +81,14 @@ define([

serialize: function() {
var res = {};
this.el.serializeArray().forEach(function(field) {
field.value = field.value.trim();
if (field.name in res) {
Array.isArray(res[field.name]) && (res[field.name] = [res[field.name]]);
res[field.name].push(field.value);
this.$('[name]').each(function(i, el) {
if (el.type === 'file') {
res[el.name] = el.files[0];
} else if (el.type === 'checkbox') {
if (!res[el.name]) res[el.name] = [];
el.checked && res[el.name].push(el.value);
} else {
res[field.name] = field.value;
res[el.name] = el.value;
}
});
return res;
Expand Down
54 changes: 50 additions & 4 deletions trunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

if (typeof define === 'function' && define.amd) {
// AMD module
define('trunk', ['jquery', 'vjs'], function($, vjs) {
return factory(root, $, vjs)
define('trunk', ['jquery'], function($) {
return factory(root, $)
});
} else {
root.Trunk = factory(root, jQuery, vjs);
root.Trunk = factory(root, jQuery);
}

})(window, function(win, $, vjs) {
})(window, function(win, $) {

// Caller as array method
var array = [];
Expand Down Expand Up @@ -341,6 +341,52 @@
}
});

var vjs = (function() {

/**
* Convert template string to javascript code, return a function which
* has a parameter data and return html string.
*/
var vjs = function(str) {
if (!str) return;
str = "var out = '" + str.replace(/\s*\n\s*/g, '') + "'";
str = str.replace(getReg.statement(), "';$1out+='");
str = str.replace(getReg.express(), "'+($1)+'");
str += ";return out;";
return new Function(vjs.global, str);
};

var regRules = {
statement: '\\s([\\s\\S]+?)', // Inertia match
express: '-\\s([\\s\\S]+?)'
};

var getReg = {};

for (var i in regRules) {
(function(i) {
getReg[i] = function() {
var _reg = new RegExp(vjs.leftTag + regRules[i] + vjs.rightTag, 'g');
getReg[i] = function() {
return _reg;
}
return _reg;
}
})(i);
}

/**
* Global variable name in template
*/
vjs.global = 'data';


vjs.leftTag = '<#';
vjs.rightTag = '#>';

return vjs;
})();


var View = function(prop) {

Expand Down

0 comments on commit 485f957

Please sign in to comment.