Skip to content

Commit 14b06d2

Browse files
committed
Re-render when changed
1 parent 1232f00 commit 14b06d2

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

model-bind.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
};
290290
var uiEvents = bindObj.uiEvents || {};
291291
uiEvents.change = uiEvents.change || function (model, element, ui, pointerPath) {
292-
return pointerPath.split('/').length <= 2;
292+
return pointerPath.split('/').length <= 2 && ui.jsonType(pointerPath) !== 'object';
293293
};
294294

295295
this.bindDom = function (context, model, element) {
@@ -519,7 +519,16 @@
519519
if (this._model === model) {
520520
result._usedBindings = this._usedBindings.concat(result._usedBindings);
521521
}
522-
result.ui = (typeof uiPath === 'string') ? this.ui.path(uiPath) : this.ui;
522+
if (typeof uiPath !== 'string') {
523+
uiPath = '';
524+
if (!this._model || (model !== this._model) && model._root === this._model._root) {
525+
uiPath = model._path.replace(/.*\//, '');
526+
}
527+
}
528+
result.ui = this.ui.path(uiPath);
529+
if (result.ui.jsonType() !== 'object') {
530+
result.ui.set({});
531+
}
523532
return result;
524533
},
525534
selectBinding: function (model, tag, attrs) {
@@ -582,17 +591,19 @@
582591
}, callback);
583592
},
584593
_updateDom: function (element, tag, attrs, callback) {
594+
var thisContext = this;
585595
var model = element.boundJsonModel;
586596
var binding = element.boundBinding;
587597
var context = element.boundContext;
588598

599+
var oldRootState = model._root.state;
589600
var innerHtml = binding.html(model, tag, attrs, this.ui, processHtml);
590601
if (typeof innerHtml === 'string') {
591602
processHtml(null, innerHtml);
592603
}
593604

594605
function processHtml(error, innerHtml) {
595-
innerHtml = innerHtml.replace(magicRegex, function (match, data) {
606+
var replacedHtml = innerHtml.replace(magicRegex, function (match, data) {
596607
try {
597608
data = JSON.parse(data);
598609
} catch (e) {
@@ -608,12 +619,30 @@
608619
});
609620
// DEBUG
610621
if (tag !== 'html' && tag !== 'body') {
611-
innerHtml = '<span class="debug">' + context._usedBindings.length + ' bindings used</span>' + innerHtml;
622+
replacedHtml = '<span class="debug">' + context._usedBindings.length + ' bindings used ' + context.ui._path + '</span>' + replacedHtml;
612623
}
613624

614625
var hostElement = element.cloneNode(false);
615-
hostElement.innerHTML = innerHtml;
626+
hostElement.innerHTML = replacedHtml;
616627
context._coerceDom(element, hostElement, null, function (err) {
628+
if (oldRootState !== model._root.state) {
629+
console.log('Model changed during rendering: ' + model.url());
630+
var newHtml = binding.html(model, tag, attrs, thisContext.ui, function (err2, newHtml) {
631+
if (newHtml === innerHtml) {
632+
return callback(error || err);
633+
}
634+
console.log('Re-rendering');
635+
return thisContext._updateDom(element, tag, attrs, callback);
636+
});
637+
if (typeof newHtml === 'string') {
638+
if (newHtml === innerHtml) {
639+
return callback(error || err);
640+
}
641+
console.log('Re-rendering');
642+
return thisContext._updateDom(element, tag, attrs, callback);
643+
}
644+
return;
645+
}
617646
callback(error || err);
618647
});
619648
}

model.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@
279279
var thisRootModel = this;
280280
this.dataStore = dataStore;
281281
this.storeKey = storeKey;
282+
this.state = Date.now(); // Just an arbitrary number
282283

283284
var pendingPoke = null;
284285
function pokeNow() {
@@ -403,6 +404,7 @@
403404
}
404405

405406
this.setPathValue = function (path, newValue) {
407+
this.state--;
406408
pokeStore();
407409
if (!path) {
408410
value = newValue;

0 commit comments

Comments
 (0)