sodajs icon indicating copy to clipboard operation
sodajs copied to clipboard

Attributes typeof 'function' or 'object'

Open godtail opened this issue 7 years ago • 1 comments

Description

Typeof function

When I use a attribute name 'link' like this {{ item.link }}, and passed a object { item:[] }, after parse it returns a function like this: function (a){for(var b={},c=0,d=this.length;c<d;c++)for(var e in a)if(a[e](this[c])){b[e]=this[c];delete a[e];break}return b}.

The link function is generation by mootools(item is an Array, and mootools changed the Array.prototype).

So, I need avoid to use link or other special attributes, It's cost a lot of time to check. but for another scene, useful for bind func on onclick.

<button onclick="{{ item.clickFunc }}"></button>

Type of object

For special scene, It's need return the encoded JSON.

<ul class="menu" data-setting="{{ menu.setting }}"></ul>

Now, only for the TextNode.

// parseTextNode
if (node.nodeType === (node.TEXT_NODE || 3)) {
       node.nodeValue = node.nodeValue.replace(VALUE_OUT_REG, (item, $1) => {
               var value = this.parseSodaExpression($1, scope);
               if (typeof value === "object") {
                     value = JSON.stringify(value, null, 2)
                }
                return value;
       });
}

Suggest

From src/soda.js, _getValue function

if (typeof data[attrStr] !== "undefined") {
    rValue = data[attrStr];
}

Ideas

Just ideas, need discuss.

  1. We always focus on the passed objects(sodaScope), and don't care with the attributes on there prototype, add hasOwnProperty to check attributes.

  2. ~~check the element attributes name like 'onclick', bad way!!!~~

  3. typeof object, use JSON.stringify.

godtail avatar Jul 14 '17 08:07 godtail

Thank @godtail . It's a good suggestion, Next version we will optimize the output function.

dorsywang avatar Jul 19 '17 09:07 dorsywang