Skip to content

Commit

Permalink
savepoint, freak project is born
Browse files Browse the repository at this point in the history
  • Loading branch information
atmin committed Jun 25, 2014
1 parent 75d331a commit af6e561
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 31 deletions.
91 changes: 72 additions & 19 deletions build/jtmpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 114,10 @@ On page ready, process jtmpl targets
// return loadModel(document.querySelector(src).innerHTML);
// }

jtmpl(t, t.getAttribute('data-template'), t.getAttribute('data-model'));
jtmpl(t,
document.querySelector(t.getAttribute('data-template')).innerHTML,
j.loadModel(document.querySelector(t.getAttribute('data-model')).innerHTML)
);
}
});

Expand Down Expand Up @@ -526,6 529,8 @@ Return documentFragment
return fragment;
};



/*
Initialize `obj.__`.
Expand Down Expand Up @@ -645,7 650,7 @@ If current context is an Array, all standard props/methods are there:
result :

// Single value
formatter(result);
formatter(result);
}

else {
Expand Down Expand Up @@ -781,21 786,28 @@ If current context is an Array, all standard props/methods are there:

splice: function() {
var length = this.length;
var result = [].splice.apply(this, arguments);
var result;

if (arguments[1]) {
notify('del', arguments[0], arguments[1]);
}

if (arguments.length > 2) {
notify('ins', arguments[0], arguments.length - 2);
}

result = [].splice.apply(this, arguments);

while (length < this.length) {
bindProp(length);
length ;
}

while (length > this.length) {
delete model[length];
delete this[length];
length--;
}
if (arguments[1]) {
notify('del', arguments[0], arguments[1]);
}
if (arguments.length > 2) {
notify('ins', arguments[0], arguments.length - 2);
}

return result;
}
};
Expand Down Expand Up @@ -865,7 877,13 @@ Notifies `callback` passing new value, when `obj[prop]` changes.
Each rule is a function, args passed (tag, node, attr, model, options)
It MUST return either:
tag: text between delimiters, {{tag}}
node: DOM node, where tag is found
attr: node attribute or null, if node contents
model: bound model
options: configuration options
It must return either:
* falsy value - no match
Expand All @@ -875,19 893,43 @@ It MUST return either:
// Set new context, default - original model
model: set_new_context_object
// Parse until {{/tagName}} ...
block: 'tagName'
// ... then call `replace`
// Return replace block tag contents
replace: function(tmpl, parent) { ... }
// React on model[prop] change
react: function(val) { ... }
// React on insertion/deletion
arrayReact: function(type, index, count) { ... }
}
*/

j.rules = [


/*
### onevent="{{handler}}"
Attach event listener for the 'event' event, remove the attribute
*/

function (tag, node, attr, model, options) {
var tagmatch = tag.match(RE_IDENTIFIER);
var attrmatch = attr && attr.match(new RegExp('on' RE_SRC_IDENTIFIER));

if (tagmatch && attrmatch) {
// Remove 'onevent' attribute
node.setAttribute(attr, null);
// TODO: use event delegation
node.addEventListener(attrmatch[1], model[tag]);
}
},



/*
### class="{{some-class}}"
Expand Down Expand Up @@ -960,6 1002,8 @@ Can be bound to text node
var length = 0;

var arrayReact = function(type, index, count) {
// console.log('arrayReact ' type ', ' index ', ' count);
var obj = model;
var parent = anchor.parentNode;
var anchorIndex = [].indexOf.call(parent.childNodes, anchor);
var pos = anchorIndex - length index * template.childNodes.length;
Expand All @@ -972,7 1016,7 @@ Can be bound to text node

for (i = 0, fragment = document.createDocumentFragment();
i < count; i ) {
fragment.appendChild(j.compile(template, model[prop][index i]));
fragment.appendChild(j.compile(template, obj[prop][index i]));
}

parent.insertBefore(fragment, parent.childNodes[pos]);
Expand All @@ -994,8 1038,17 @@ Can be bound to text node

var update = function(i) {
return function() {
arrayReact('ins', i, 1);
arrayReact('del', i 1, 1);
// arrayReact('del', i, 1);
// arrayReact('ins', i, 1);
// model[prop].__(i, null, true);
var parent = anchor.parentNode;
var anchorIndex = [].indexOf.call(parent.childNodes, anchor);
var pos = anchorIndex - length i * template.childNodes.length;

parent.replaceChild(
j.compile(template, model[prop][i]),
parent.childNodes[pos]
);
};
};

Expand All @@ -1004,7 1057,7 @@ Can be bound to text node
var arrayWatchers;

if (typeof model[prop] === 'object' && model[prop].__) {
// Capture dunder
// Capture arrayWatchers
arrayWatchers = model[prop].__.arrayWatchers;
}

Expand Down
Loading

0 comments on commit af6e561

Please sign in to comment.