如何判断视图是否已经渲染? javascript

发布于 2024-12-22 18:50:12 字数 608 浏览 0 评论 0原文

我正在使用backbone.js 创建我的应用程序

,如下所示,我有一个用于渲染布局的layoutView,以及布局中的一个迷你配置文件。

我遇到的问题是时间安排。在触发“renderProfile”方法之前,我需要先完成“render”方法。我怎样才能做到这一点?

Onethingaday.Views.Home ||= {}

class Onethingaday.Views.Home.LayoutView extends Backbone.View
  template: JST["backbone/templates/home/layout"]

  initialize: ->
    @options.user.bind('change',@render,@renderProfile, @)

  renderProfile: ->
    view = new Onethingaday.Views.Shared.MiniProfileView
      user: @options.user

    @$('.profile').html view.render().el

  render: ->
    $(@el).html(@template())
    @

I am creating my application using backbone.js

As seen below I have a layoutView which I use to render the layout and also a mini profile within the layout.

The issue I have is with timing. I need to have the 'render' method complete first before triggering 'renderProfile' method. How can I do that?

Onethingaday.Views.Home ||= {}

class Onethingaday.Views.Home.LayoutView extends Backbone.View
  template: JST["backbone/templates/home/layout"]

  initialize: ->
    @options.user.bind('change',@render,@renderProfile, @)

  renderProfile: ->
    view = new Onethingaday.Views.Shared.MiniProfileView
      user: @options.user

    @$('.profile').html view.render().el

  render: ->
    $(@el).html(@template())
    @

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

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

发布评论

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

评论(1

铃予 2024-12-29 18:50:12

你的情况就是我为 Backbone 编写 LayoutManager 的原因, http://github.com/tbranyen/backbone.layoutmanager。

您应该做的是将子视图与主(布局)视图分开。

因此,在您的路由回调中,您会看到类似这样的内容:

// Initialize a Layout View
var profile = new Onethingaday.Views.Home.LayoutView();
// Initialize a MiniProfile View
var miniProfile = new Onethingaday.Views.Shared.MiniProfileView({
  model: user
});

// This appears synchronous in your code, so this should work fine
$(profile.render().el).find(".profile").html(miniProfile.render());

我恳请您研究我的库,因为我认为子视图与布局关联的声明方式确实非常优雅。

Your situation is why I wrote LayoutManager for Backbone, http://github.com/tbranyen/backbone.layoutmanager.

What you should be doing is separating your sub views from your main (layout) view.

So in your route callback you'd have something like this:

// Initialize a Layout View
var profile = new Onethingaday.Views.Home.LayoutView();
// Initialize a MiniProfile View
var miniProfile = new Onethingaday.Views.Shared.MiniProfileView({
  model: user
});

// This appears synchronous in your code, so this should work fine
$(profile.render().el).find(".profile").html(miniProfile.render());

I would implore you to investigate my library, as I think the declarative manner in which sub views are associated to layouts is really quite elegant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文