0

我有这段代码——我只需要在将在模板中呈现的 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>
4

1 回答 1

0

我是 Sammy 的新手,所以对此持保留态度。我认为它失败了,因为你的第一个 .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');
     });
});
于 2012-07-29T16:09:28.660 回答