Git Product home page Git Product logo

Comments (9)

popomore avatar popomore commented on July 20, 2024

版本为 [email protected]

from velocity.js.

popomore avatar popomore commented on July 20, 2024

看了下,从 0.3.4 开始有这个问题

from velocity.js.

shepherdwind avatar shepherdwind commented on July 20, 2024

出现这个问题是因为我在内部开放出this.eval用于解析vm字符串,在调用context和macros的方法的时候,把this指向了velocityjs对象 src/compile/references.js#L200 。

如果不改变context里面函数对应的this上线文,那么在函数里面就没法调用this.eval方法了。

from velocity.js.

shepherdwind avatar shepherdwind commented on July 20, 2024

我想到一个稍微麻烦一些的方案是,只对特定的函数改变其中的this,比如你需要调用this.eval来执行velocityjs解析,那么你的context里面需要有一个方法,比如名字也是eval。

比如这样:

var velocity = require('../code/velocity.js/');
var data = 'a = $a.get()';

function b(c) {
  this.c = c;
}

b.prototype.get = function() {
  // this 指向的是b
  return this.c;
};

b.prototype.eval = function(str){
  // this 指向的是Velocity.Compile
  return this.eval(str);
};

var property = velocity.render(data, {
  a: new b(1)
});
console.log(property);

from velocity.js.

popomore avatar popomore commented on July 20, 2024

能否不改变 this,而是增加到属性上?比如所有的 this 上都增加个 eval,只要不重名。

from velocity.js.

popomore avatar popomore commented on July 20, 2024
var that = this;
ret.eval = function(str) {
  that.eval(str)
}
ret = ret.apply(ret, args);

如果只使用一个 eval 方法的话,是否可以这样

from velocity.js.

shepherdwind avatar shepherdwind commented on July 20, 2024

嗯,这个方案比较好,我在context传入之初就给context增加一个eval方法。

from velocity.js.

shepherdwind avatar shepherdwind commented on July 20, 2024

好了,我发布了一下0.3.9版本,并且增加了测试用例

from velocity.js.

popomore avatar popomore commented on July 20, 2024

修改神速 👍

from velocity.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.