derby-templates
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -22,3 +22,3 @@ exports.ContextMeta = ContextMeta; | ||
function Context(meta, controller, parent, unbound, expression, item, view, attributes, hooks, initHooks) { | ||
function Context(meta, controller, parent, unbound, expression) { | ||
// Required properties // | ||
@@ -45,15 +45,19 @@ | ||
// For Context::eachChild | ||
// The index of the each at render time | ||
this.item = item; | ||
// The context of the each at render time | ||
this.item = null; | ||
// For Context::viewChild | ||
// Reference to the current view | ||
this.view = view; | ||
this.view = null; | ||
// Attribute values passed to the view instance | ||
this.attributes = attributes; | ||
this.attributes = null; | ||
// MarkupHooks to be called after insert into DOM of component | ||
this.hooks = hooks; | ||
this.hooks = null; | ||
// MarkupHooks to be called immediately before init of component | ||
this.initHooks = initHooks; | ||
this.initHooks = null; | ||
// For Context::closureChild | ||
// Reference to another context established at render time by ContextClosure | ||
this.closure = null; | ||
// Used in EventModel | ||
@@ -99,4 +103,5 @@ this._id = null; | ||
// Make a context for an item in an each block | ||
Context.prototype.eachChild = function(expression, index) { | ||
var context = new Context(this.meta, this.controller, this, this.unbound, expression, index); | ||
Context.prototype.eachChild = function(expression, item) { | ||
var context = new Context(this.meta, this.controller, this, this.unbound, expression); | ||
context.item = item; | ||
this.meta.addItemContext(context); | ||
@@ -107,5 +112,16 @@ return context; | ||
Context.prototype.viewChild = function(view, attributes, hooks, initHooks) { | ||
return new Context(this.meta, this.controller, this, this.unbound, null, null, view, attributes, hooks, initHooks); | ||
var context = new Context(this.meta, this.controller, this, this.unbound); | ||
context.view = view; | ||
context.attributes = attributes; | ||
context.hooks = hooks; | ||
context.initHooks = initHooks; | ||
return context; | ||
}; | ||
Context.prototype.closureChild = function(closure) { | ||
var context = new Context(this.meta, this.controller, this, this.unbound); | ||
context.closure = closure; | ||
return context; | ||
}; | ||
Context.prototype.forRelative = function(expression) { | ||
@@ -148,3 +164,7 @@ var context = this; | ||
while (context) { | ||
// Find the closest view | ||
// When a context with a `closure` property is encountered, skip to its | ||
// parent context rather than returning the nearest view's. This reference | ||
// is created by wrapping a template in a ContextClosure template | ||
if (context.closure) return context.closure.parent; | ||
// Find the closest view and return the containing context | ||
if (context.view) return context.parent; | ||
@@ -151,0 +171,0 @@ context = context.parent; |
@@ -20,3 +20,3 @@ var saddle = require('saddle'); | ||
exports.ViewParent = ViewParent; | ||
exports.TemplateClosure = TemplateClosure; | ||
exports.ContextClosure = ContextClosure; | ||
@@ -366,2 +366,6 @@ exports.Views = Views; | ||
// Without a ContextClosure, ViewParent will return the nearest context that | ||
// is the parent of a view instance. When a context with a `closure` property | ||
// is encountered first, ViewParent will find the specific referenced context, | ||
// even if it is further up the context hierarchy. | ||
function ViewParent(template) { | ||
@@ -398,30 +402,37 @@ this.template = template; | ||
// This template type wraps a template in a fixed context. It should be | ||
// created dynamically during rendering in order to pass the template with a | ||
// given context, similar to a closure. Unlike other template types, instances | ||
// can only be created dynamically and cannot be serialized. | ||
function TemplateClosure(template, context) { | ||
// At render time, this template creates a context child and sets its | ||
// `closure` property to a fixed reference. It is used in combination with | ||
// ViewParent in order to control which context is returned. | ||
// | ||
// Instances of this template cannot be serialized. It is intended for use | ||
// dynamically during rendering only. | ||
function ContextClosure(template, context) { | ||
this.template = template; | ||
this.context = context; | ||
} | ||
TemplateClosure.prototype = Object.create(saddle.Template.prototype); | ||
TemplateClosure.prototype.constructor = TemplateClosure; | ||
TemplateClosure.prototype.serialize = function() { | ||
throw new Error('TemplateClosure cannot be serialized'); | ||
ContextClosure.prototype = Object.create(saddle.Template.prototype); | ||
ContextClosure.prototype.constructor = ContextClosure; | ||
ContextClosure.prototype.serialize = function() { | ||
throw new Error('ContextClosure cannot be serialized'); | ||
}; | ||
TemplateClosure.prototype.get = function(ignoredContext, unescaped) { | ||
return this.template.get(this.context, unescaped); | ||
ContextClosure.prototype.get = function(context, unescaped) { | ||
var closureContext = context.closureChild(this.context); | ||
return this.template.get(closureContext, unescaped); | ||
}; | ||
TemplateClosure.prototype.getFragment = function(ignoredContext, binding) { | ||
return this.template.getFragment(this.context, binding); | ||
ContextClosure.prototype.getFragment = function(context, binding) { | ||
var closureContext = context.closureChild(this.context); | ||
return this.template.getFragment(closureContext, binding); | ||
}; | ||
TemplateClosure.prototype.appendTo = function(parent, ignoredContext) { | ||
this.template.appendTo(parent, this.context); | ||
ContextClosure.prototype.appendTo = function(parent, context) { | ||
var closureContext = context.closureChild(this.context); | ||
this.template.appendTo(parent, closureContext); | ||
}; | ||
TemplateClosure.prototype.attachTo = function(parent, node, ignoredContext) { | ||
return this.template.attachTo(parent, node, this.context); | ||
ContextClosure.prototype.attachTo = function(parent, node, context) { | ||
var closureContext = context.closureChild(this.context); | ||
return this.template.attachTo(parent, node, closureContext); | ||
}; | ||
TemplateClosure.prototype.dependencies = function(ignoredContext, options) { | ||
ContextClosure.prototype.dependencies = function(context, options) { | ||
if (DependencyOptions.shouldIgnoreTemplate(this, options)) return; | ||
return this.template.dependencies(this.context, options); | ||
var closureContext = context.closureChild(this.context); | ||
return this.template.dependencies(closureContext, options); | ||
}; | ||
@@ -428,0 +439,0 @@ |
{ | ||
"name": "derby-templates", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66481
1704
13