-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from bigopon/templating-binding-let
feat(Let): support let element
- Loading branch information
Showing
7 changed files
with
614 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { | ||
connectable, | ||
enqueueBindingConnect, | ||
sourceContext | ||
} from 'aurelia-binding'; | ||
|
||
export class LetExpression { | ||
/** | ||
* @param {ObserverLocator} observerLocator | ||
* @param {string} targetProperty | ||
* @param {Expression} sourceExpression | ||
* @param {any} lookupFunctions | ||
* @param {boolean} toBindingContext indicates let binding result should be assigned to binding context | ||
*/ | ||
constructor(observerLocator, targetProperty, sourceExpression, lookupFunctions, toBindingContext) { | ||
this.observerLocator = observerLocator; | ||
this.sourceExpression = sourceExpression; | ||
this.targetProperty = targetProperty; | ||
this.lookupFunctions = lookupFunctions; | ||
this.toBindingContext = toBindingContext; | ||
} | ||
|
||
createBinding() { | ||
return new Let( | ||
this.observerLocator, | ||
this.sourceExpression, | ||
this.targetProperty, | ||
this.lookupFunctions, | ||
this.toBindingContext | ||
); | ||
} | ||
} | ||
|
||
@connectable() | ||
export class Let { | ||
/** | ||
* @param {ObserverLocator} observerLocator | ||
* @param {Expression} sourceExpression | ||
* @param {Function | Element} target | ||
* @param {string} targetProperty | ||
* @param {*} lookupFunctions | ||
* @param {boolean} toBindingContext indicates let binding result should be assigned to binding context | ||
*/ | ||
constructor(observerLocator, sourceExpression, targetProperty, lookupFunctions, toBindingContext) { | ||
this.observerLocator = observerLocator; | ||
this.sourceExpression = sourceExpression; | ||
this.targetProperty = targetProperty; | ||
this.lookupFunctions = lookupFunctions; | ||
this.source = null; | ||
this.target = null; | ||
this.toBindingContext = toBindingContext; | ||
} | ||
|
||
updateTarget() { | ||
const value = this.sourceExpression.evaluate(this.source, this.lookupFunctions); | ||
this.target[this.targetProperty] = value; | ||
} | ||
|
||
call(context) { | ||
if (!this.isBound) { | ||
return; | ||
} | ||
if (context === sourceContext) { | ||
this.updateTarget(); | ||
return; | ||
} | ||
throw new Error(`Unexpected call context ${context}`); | ||
} | ||
|
||
/** | ||
* @param {Scope} source Binding context | ||
*/ | ||
bind(source) { | ||
if (this.isBound) { | ||
if (this.source === source) { | ||
return; | ||
} | ||
this.unbind(); | ||
} | ||
|
||
this.isBound = true; | ||
this.source = source; | ||
this.target = this.toBindingContext ? source.bindingContext : source.overrideContext; | ||
|
||
if (this.sourceExpression.bind) { | ||
this.sourceExpression.bind(this, source, this.lookupFunctions); | ||
} | ||
|
||
enqueueBindingConnect(this); | ||
} | ||
|
||
unbind() { | ||
if (!this.isBound) { | ||
return; | ||
} | ||
this.isBound = false; | ||
if (this.sourceExpression.unbind) { | ||
this.sourceExpression.unbind(this, this.source); | ||
} | ||
this.source = null; | ||
this.target = null; | ||
this.unobserve(true); | ||
} | ||
|
||
connect() { | ||
if (!this.isBound) { | ||
return; | ||
} | ||
this.updateTarget(); | ||
this.sourceExpression.connect(this, this.source); | ||
} | ||
} |
Oops, something went wrong.