萨米的电话

Callbacks in Sammy

本文关键字:电话      更新时间:2024-05-06

我有这样的代码-我只需要在DOM元素上添加一个类,该类将在模板中呈现。然而,这并没有奏效!

    this.get('#/:post_id', function(context){
            context.app.swap('');
         this.load('/post/' +  this.params['post_id'])
             .then(function(candidates){
                  $.each(candidates[1], function(i, candidate){
                      context.render("static/templates/candidate.template", {candidate:candidate})
                     .appendTo(context.$element());
             });
         })
        .then(function(){
            $('h3').addClass('custom_class');
         });

候选模板:

<div>
  <h3>Name : <%= candidate.name %> </h3>
  ...
  ...
</div>

我是萨米的新手,所以对此持怀疑态度。我认为它失败了,因为你的第一个.then()没有返回任何东西(比如上下文),这意味着下一个.then

这样的东西行得通吗?

this.get('#/:post_id', function(context){
        context.app.swap('');
     this.load('/post/' +  this.params['post_id'])
         .then(function(candidates){
              $.each(candidates[1], function(i, candidate){
                  context.render("static/templates/candidate.template", {candidate:candidate})
                 .appendTo(context.$element());
         });
         return context;
     })
    .then(function(){
        $('h3').addClass('custom_class');
     });
});