如何在使用 Backbone.js 渲染视图后触发事件?

发布于 2024-12-27 22:13:59 字数 725 浏览 3 评论 0原文

我有很多使用模板的视图。视图的渲染工作完美,现在在我的路由器中,我正在寻找一种在渲染所有视图时触发事件的方法! 我使用了像 LAB.js 这样的 js 加载器,但没有任何效果!

渲染完成后,我将事件输入到 Firebug 控制台中,它就可以工作了!

我如何以及在这里放置我的事件,以便在呈现所有视图时触发它!

**我的活动:**

$('div[id ^="solfg_"]').mobilyblocks();

**路由器:**

(function () {

 window.AppRouter = Backbone.Router.extend({
 routes: {
  ""  : "init"
 },

 init: function(){

   this.solfsmodel = new Solfs();
   this.solfsmodel.fetch(); 
   this.solfsView = new SolfsView({model: this.solfsmodel});

 }

});
var app_router = new AppRouter;
Backbone.history.start();
}(jQuery));

谢谢

更新: 同样的问题

I have many Views whos work with templates. The Rendering with the Views work perfectly, now into my Router i'm seeking to a way to trigger an Event when all Views rendered!
I used js loader like LAB.js but nothing work!

After all rendered i enter the event into the firebug console and it's work!

How and Here can i place my event so that it's trigger when all views rendered!

**My Event : **

$('div[id ^="solfg_"]').mobilyblocks();

**Router : **

(function () {

 window.AppRouter = Backbone.Router.extend({
 routes: {
  ""  : "init"
 },

 init: function(){

   this.solfsmodel = new Solfs();
   this.solfsmodel.fetch(); 
   this.solfsView = new SolfsView({model: this.solfsmodel});

 }

});
var app_router = new AppRouter;
Backbone.history.start();
}(jQuery));

Thank you

update : same problems

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一花一树开 2025-01-03 22:13:59

我发现解决方案只是使用 jquery 中的 $.when().then() ,真的很神奇,我从未见过这个 jquery 函数。

*我的解决方案:*

(function () {

 window.AppRouter = Backbone.Router.extend({
 routes: {
   ""  : "run"
 },

initialize: function(){
  this.solfsmodel = new Solfs();

  this.solfsView = new SolfsView({model: this.solfsmodel});

},
run: function(){
  $.when(
     this.solfsmodel.fetch(); 
  ).then(function(){
     *$('div[id ^="solfg_"]').mobilyblocks();*
  });
}
});
var app_router = new AppRouter;
Backbone.history.start();
}(jQuery));

I found the solution just use $.when().then() from jquery, really amazing that i never saw this jquery function.

*My Solution : *

(function () {

 window.AppRouter = Backbone.Router.extend({
 routes: {
   ""  : "run"
 },

initialize: function(){
  this.solfsmodel = new Solfs();

  this.solfsView = new SolfsView({model: this.solfsmodel});

},
run: function(){
  $.when(
     this.solfsmodel.fetch(); 
  ).then(function(){
     *$('div[id ^="solfg_"]').mobilyblocks();*
  });
}
});
var app_router = new AppRouter;
Backbone.history.start();
}(jQuery));
鲜血染红嫁衣 2025-01-03 22:13:59

如果您只需要等待获取集合,您可以使用 fetch 方法的 success 回调(来源:http://backbonejs.org/#Collection-fetch)。

在使用其他库之前最好使用 Backbone.js 方法。

所以你的代码应该是这样的:

(function () {

    window.AppRouter = Backbone.Router.extend({
        routes: {
            ""  : "run"
        },

        initialize: function(){
            this.solfsmodel = new Solfs();
            this.solfsView = new SolfsView({model: this.solfsmodel});
        },

        run: function(){
            this.solfsmodel.fetch({
                success: function () { 
                    $('div[id ^="solfg_"]').mobilyblocks();
                }
            );
        }
    });
    var app_router = new AppRouter;
    Backbone.history.start();
}(jQuery));

You can use the success callback of fetch method if you just need to wait that the collection is fetched (source: http://backbonejs.org/#Collection-fetch).

It's preferable to use Backbone.js method before use other librairies.

So your code should look like:

(function () {

    window.AppRouter = Backbone.Router.extend({
        routes: {
            ""  : "run"
        },

        initialize: function(){
            this.solfsmodel = new Solfs();
            this.solfsView = new SolfsView({model: this.solfsmodel});
        },

        run: function(){
            this.solfsmodel.fetch({
                success: function () { 
                    $('div[id ^="solfg_"]').mobilyblocks();
                }
            );
        }
    });
    var app_router = new AppRouter;
    Backbone.history.start();
}(jQuery));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文