Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored blaze to move ES6 version #397

Open
wants to merge 7 commits into
base: release-3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 116 additions & 103 deletions packages/blaze/attrs.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions packages/blaze/backcompat.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* global Blaze UI Handlebars ReactiveVar */
klablink marked this conversation as resolved.
Show resolved Hide resolved
/* eslint-disable no-global-assign */

UI = Blaze;

Blaze.ReactiveVar = ReactiveVar;
Expand All @@ -10,9 +13,9 @@ Handlebars._escape = Blaze._escape;

// Return these from {{...}} helpers to achieve the same as returning
// strings from {{{...}}} helpers
Handlebars.SafeString = function(string) {
Handlebars.SafeString = function (string) {
this.string = string;
};
Handlebars.SafeString.prototype.toString = function() {
Handlebars.SafeString.prototype.toString = function () {
return this.string.toString();
};
96 changes: 47 additions & 49 deletions packages/blaze/builtins.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* global Blaze HTML ReactiveVar Tracker ObserveSequence */
/* eslint-disable import/no-unresolved, no-global-assign */

import has from 'lodash.has';
import isObject from 'lodash.isobject';

Blaze._calculateCondition = function (cond) {
if (HTML.isArray(cond) && cond.length === 0)
cond = false;
return !! cond;
if (HTML.isArray(cond) && cond.length === 0) return false;
return !!cond;
};

/**
Expand All @@ -14,9 +16,9 @@ Blaze._calculateCondition = function (cond) {
* @param {Function} contentFunc A Function that returns [*renderable content*](#Renderable-Content).
*/
Blaze.With = function (data, contentFunc) {
var view = Blaze.View('with', contentFunc);
const view = Blaze.View('with', contentFunc);

view.dataVar = new ReactiveVar;
view.dataVar = new ReactiveVar();

view.onViewCreated(function () {
if (typeof data === 'function') {
Expand All @@ -41,6 +43,7 @@ Blaze.With = function (data, contentFunc) {
Blaze._attachBindingsToView = function (bindings, view) {
view.onViewCreated(function () {
Object.entries(bindings).forEach(function ([name, binding]) {
// eslint-disable-next-line no-param-reassign
view._scopeBindings[name] = new ReactiveVar();
if (typeof binding === 'function') {
view.autorun(function () {
Expand All @@ -60,7 +63,7 @@ Blaze._attachBindingsToView = function (bindings, view) {
* @param {Function} contentFunc A Function that returns [*renderable content*](#Renderable-Content).
*/
Blaze.Let = function (bindings, contentFunc) {
var view = Blaze.View('let', contentFunc);
const view = Blaze.View('let', contentFunc);
Blaze._attachBindingsToView(bindings, view);

return view;
Expand All @@ -74,17 +77,17 @@ Blaze.Let = function (bindings, contentFunc) {
* @param {Function} [elseFunc] Optional. A Function that returns [*renderable content*](#Renderable-Content). If no `elseFunc` is supplied, no content is shown in the "else" case.
*/
Blaze.If = function (conditionFunc, contentFunc, elseFunc, _not) {
var conditionVar = new ReactiveVar;
const conditionVar = new ReactiveVar();

var view = Blaze.View(_not ? 'unless' : 'if', function () {
const view = Blaze.View(_not ? 'unless' : 'if', function () {
return conditionVar.get() ? contentFunc() :
(elseFunc ? elseFunc() : null);
});
view.__conditionVar = conditionVar;
view.onViewCreated(function () {
this.autorun(function () {
var cond = Blaze._calculateCondition(conditionFunc());
conditionVar.set(_not ? (! cond) : cond);
const cond = Blaze._calculateCondition(conditionFunc());
conditionVar.set(_not ? (!cond) : cond);
}, this.parentView, 'condition');
});

Expand All @@ -99,7 +102,7 @@ Blaze.If = function (conditionFunc, contentFunc, elseFunc, _not) {
* @param {Function} [elseFunc] Optional. A Function that returns [*renderable content*](#Renderable-Content). If no `elseFunc` is supplied, no content is shown in the "else" case.
*/
Blaze.Unless = function (conditionFunc, contentFunc, elseFunc) {
return Blaze.If(conditionFunc, contentFunc, elseFunc, true /*_not*/);
return Blaze.If(conditionFunc, contentFunc, elseFunc, true /* _not */);
};

/**
Expand All @@ -123,11 +126,11 @@ Blaze.Unless = function (conditionFunc, contentFunc, elseFunc) {
* in the sequence.
*/
Blaze.Each = function (argFunc, contentFunc, elseFunc) {
var eachView = Blaze.View('each', function () {
var subviews = this.initialSubviews;
const eachView = Blaze.View('each', function () {
const subviews = this.initialSubviews;
this.initialSubviews = null;
if (this._isCreatedForExpansion) {
this.expandedValueDep = new Tracker.Dependency;
this.expandedValueDep = new Tracker.Dependency();
this.expandedValueDep.depend();
}
return subviews;
Expand All @@ -138,17 +141,13 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {
eachView.stopHandle = null;
eachView.contentFunc = contentFunc;
eachView.elseFunc = elseFunc;
eachView.argVar = new ReactiveVar;
eachView.argVar = new ReactiveVar();
eachView.variableName = null;

// update the @index value in the scope of all subviews in the range
var updateIndices = function (from, to) {
if (to === undefined) {
to = eachView.numItems - 1;
}

for (var i = from; i <= to; i++) {
var view = eachView._domrange.members[i].view;
const updateIndices = function (from, to = eachView.numItems - 1) {
for (let i = from; i <= to; i++) {
const { view } = eachView._domrange.members[i];
view._scopeBindings['@index'].set(i);
}
};
Expand All @@ -160,7 +159,7 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {
eachView.autorun(function () {
// argFunc can return either a sequence as is or a wrapper object with a
// _sequence and _variable fields set.
var arg = argFunc();
let arg = argFunc();
if (isObject(arg) && has(arg, '_sequence')) {
eachView.variableName = arg._variable || null;
arg = arg._sequence;
Expand All @@ -172,9 +171,9 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {
eachView.stopHandle = ObserveSequence.observe(function () {
return eachView.argVar.get();
}, {
addedAt: function (id, item, index) {
addedAt(id, item, index) {
Tracker.nonreactive(function () {
var newItemView;
let newItemView;
if (eachView.variableName) {
// new-style #each (as in {{#each item in items}})
// doesn't create a new data context
Expand All @@ -185,7 +184,7 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {

eachView.numItems++;

var bindings = {};
const bindings = {};
bindings['@index'] = index;
if (eachView.variableName) {
bindings[eachView.variableName] = item;
Expand All @@ -200,15 +199,15 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {
eachView.inElseMode = false;
}

var range = Blaze._materializeView(newItemView, eachView);
const range = Blaze._materializeView(newItemView, eachView);
eachView._domrange.addMember(range, index);
updateIndices(index);
} else {
eachView.initialSubviews.splice(index, 0, newItemView);
}
});
},
removedAt: function (id, item, index) {
removedAt(id, item, index) {
Tracker.nonreactive(function () {
eachView.numItems--;
if (eachView.expandedValueDep) {
Expand All @@ -220,20 +219,20 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {
eachView.inElseMode = true;
eachView._domrange.addMember(
Blaze._materializeView(
Blaze.View('each_else',eachView.elseFunc),
Blaze.View('each_else', eachView.elseFunc),
eachView), 0);
}
} else {
eachView.initialSubviews.splice(index, 1);
}
});
},
changedAt: function (id, newItem, oldItem, index) {
changedAt(id, newItem, oldItem, index) {
Tracker.nonreactive(function () {
if (eachView.expandedValueDep) {
eachView.expandedValueDep.changed();
} else {
var itemView;
let itemView;
if (eachView._domrange) {
itemView = eachView._domrange.getMember(index).view;
} else {
Expand All @@ -247,7 +246,7 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {
}
});
},
movedTo: function (id, item, fromIndex, toIndex) {
movedTo(id, item, fromIndex, toIndex) {
Tracker.nonreactive(function () {
if (eachView.expandedValueDep) {
eachView.expandedValueDep.changed();
Expand All @@ -256,13 +255,13 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {
updateIndices(
Math.min(fromIndex, toIndex), Math.max(fromIndex, toIndex));
} else {
var subviews = eachView.initialSubviews;
var itemView = subviews[fromIndex];
const subviews = eachView.initialSubviews;
const itemView = subviews[fromIndex];
subviews.splice(fromIndex, 1);
subviews.splice(toIndex, 0, itemView);
}
});
}
},
});

if (eachView.elseFunc && eachView.numItems === 0) {
Expand All @@ -273,17 +272,16 @@ Blaze.Each = function (argFunc, contentFunc, elseFunc) {
});

eachView.onViewDestroyed(function () {
if (eachView.stopHandle)
eachView.stopHandle.stop();
if (eachView.stopHandle) eachView.stopHandle.stop();
});

return eachView;
};

Blaze._TemplateWith = function (arg, contentFunc) {
var w;
let w;

var argFunc = arg;
let argFunc = arg;
if (typeof arg !== 'function') {
argFunc = function () {
return arg;
Expand All @@ -301,20 +299,19 @@ Blaze._TemplateWith = function (arg, contentFunc) {
//
// To make this better, reconsider _InOuterTemplateScope as a primitive.
// Longer term, evaluate expressions in the proper lexical scope.
var wrappedArgFunc = function () {
var viewToEvaluateArg = null;
const wrappedArgFunc = function () {
let viewToEvaluateArg = null;
if (w.parentView && w.parentView.name === 'InOuterTemplateScope') {
viewToEvaluateArg = w.parentView.originalParentView;
}
if (viewToEvaluateArg) {
return Blaze._withCurrentView(viewToEvaluateArg, argFunc);
} else {
return argFunc();
}
return argFunc();
};

var wrappedContentFunc = function () {
var content = contentFunc.call(this);
const wrappedContentFunc = function () {
let content = contentFunc.call(this);

// Since we are generating the Blaze._TemplateWith view for the
// user, set the flag on the child view. If `content` is a template,
Expand All @@ -335,15 +332,17 @@ Blaze._TemplateWith = function (arg, contentFunc) {
};

Blaze._InOuterTemplateScope = function (templateView, contentFunc) {
var view = Blaze.View('InOuterTemplateScope', contentFunc);
var parentView = templateView.parentView;
const view = Blaze.View('InOuterTemplateScope', contentFunc);
let { parentView } = templateView;

// Hack so that if you call `{{> foo bar}}` and it expands into
// `{{#with bar}}{{> foo}}{{/with}}`, and then `foo` is a template
// that inserts `{{> Template.contentBlock}}`, the data context for
// `Template.contentBlock` is not `bar` but the one enclosing that.
if (parentView.__isTemplateWith)
if (parentView.__isTemplateWith) {
// eslint-disable-next-line prefer-destructuring
parentView = parentView.parentView;
}

view.onViewCreated(function () {
this.originalParentView = this.parentView;
Expand All @@ -352,4 +351,3 @@ Blaze._InOuterTemplateScope = function (templateView, contentFunc) {
});
return view;
};

Loading