Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeanEngine: issue #463 增加 hook 函数可能造成死循环调用的 FAQ 说明 #848

Merged
merged 1 commit into from
Dec 31, 2015

Conversation

sdjcw
Copy link
Contributor

@sdjcw sdjcw commented Dec 31, 2015

最好能在 leanengine_guide 文档上给个链接,但是我发现 leanengine_guide 文档已经没有 Hook 函数 这个专门的小节,而是和 Cloud 函数合并起来了。。。。所以我不知道加在哪里合适。

@killme2008
Copy link
Contributor

👍

@sdjcw
Copy link
Contributor Author

sdjcw commented Dec 31, 2015

leanengine 0.3.0 已经发布,文档可以合并发布了。

- 对传入的 `request.object` 对象进行 `fetch` 操作。
- 重新构造传入的 `request.object` 对象,比如通过 `AV.Object.createWithoutData()` 方法。

对于上述方式产生的对象,请根据需要自行调用两个 API: `object.disableBeforeHook()` 或 `object.disableAfterHook()` ,这样对象的保存或删除动作就不会再次触发相关的 Hook 函数。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要示例代码。

这两种方法是要放在 hook 函数的最后还是开头,还是在有改动操作的地方,比如 fetch 方法后马上就调用?

比如下面的 event.preventDefault(); 就是放在最开始。

$( "a" ).click(function( event ) {
  event.preventDefault();
  $( "<div>" )
    .append( "default " + event.type + " prevented" )
    .appendTo( "#log" );
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  AV.Cloud.afterUpdate('Post', function(request) {
    // 直接修改并保存对象不会再次触发 afterUpdate Hook 函数
    request.object.set('foo', 'bar');
    request.object.save().then(function(obj) {
      // 你的业务逻辑
    });

    // 如果有 fetch 操作,则需要在新获得的对象上调用相关的 disable 方法
    // 来确保不会再次触发 Hook 函数
    request.object.fetch().then(function(obj) {
      obj.disableAfterHook();
      obj.set('foo', 'bar');
      return obj.save();
    }).then(function(obj) {
      // 你的业务逻辑
    });

    // 如果是其他方式构建对象,则需要在新构建的对象上调用相关的 disable 方法
    // 来确保不会再次触发 Hook 函数
    var obj = AV.Object.createWithoutData('Post', request.object.id);
    obj.disableAfterHook();
    obj.set('foo', 'bar');
    obj.save().then(function(obj) {
      // 你的业务逻辑
    });
  });

@myleslee
Copy link
Contributor

合并后我会把「Hook 函数」分列成一个标题,死循环会放到正文里,不会放到最后的 FAQ 里。

FAQ 以后都会转移到知识库。

myleslee added a commit that referenced this pull request Dec 31, 2015
LeanEngine: issue #463 增加 hook 函数可能造成死循环调用的 FAQ 说明
@myleslee myleslee merged commit f4daf6b into leancloud:master Dec 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants