canjs / can-stache-bindings

Binding helpers for CanJS template engines
https://canjs.com/doc/can-stache-bindings.html
MIT License
8 stars 8 forks source link

Property value setting is not working with VM values #516

Closed cherifGsoul closed 5 years ago

cherifGsoul commented 5 years ago

The following does not work: <button on:click="this.isOpen = !this.open"> Toggle </button>

Seems is only working with primitive values, setting the value this.isOpen to true or false works fine.

A CodePen https://codepen.io/cherifGsoul/pen/KYMdma for issue demonstration.

chasenlehara commented 5 years ago

The docs don’t mention being able to use !. I think this would be a nice feature.

cherifGsoul commented 5 years ago

This should be fixed in can-stache/src/expression.js

cherifGsoul commented 5 years ago

In can-stache add ! to tokens pattern:

https://github.com/canjs/can-stache/blob/83191d3c312845acc70f103d167ef378625284ca/src/expression.js#L37 -> var tokensRegExp = /('.*?'|".*?"|=|[\w\.\\\-_@\/*%!\$]+|[\(\)]|,|\~|\[|\]\s*|\s*(?=\[))/g

Update Lookup: https://github.com/canjs/can-stache/blob/83191d3c312845acc70f103d167ef378625284ca/expressions/lookup.js#L11 ->

var Lookup = function(key, root, sourceText) {
    this.inverse = false
    // Read falsy value of the key starts with "!"
    if (key.substring(1, key.length)) {
        this.key = key.slice(1);
        this.inverse = true;
    } else {
        this.key = key;
    }
    this.rootExpr = root;
    canReflect.setKeyValue(this, sourceTextSymbol, sourceText);
};

in can-stache-bindings: https://github.com/canjs/can-stache-bindings/blob/master/can-stache-bindings.js#L549 ->

var hashExprs = expr.hashExprs;
var key = Object.keys(hashExprs)[0];
var isInverse = hashExprs[key].inverse;
var value = expr.hashExprs[key].value(runScope);
var isObservableValue = canReflect.isObservableLike(value) && canReflect.isValueLike(value);
var valueToBind;
isObservableValue ? valueToBind = canReflect.getValue(value) : valueToBind = value;
runScope.set(key, isInverse ? !valueToBind : valueToBind);
cherifGsoul commented 5 years ago

Fixed as https://github.com/canjs/can-stache-bindings/pull/519