Backbone.js - 在哪里定义视图助手?

发布于 2024-12-02 13:30:24 字数 180 浏览 0 评论 0原文

最近几周我一直在尝试 Backbone.js 并进行尝试,所以有点菜鸟问题......

在backbone.js 中定义和使用视图助手的“正确”方法是什么?

据我所知,定义在模板中使用的助手的唯一真正位置是模型或集合本身。然而,当该帮助器直接返回 HTML 时,这开始感觉有点脏。

有更好的办法吗?

I've been kicking the tyres of Backbone.js and having a play around in recent weeks, so a bit of a noob question...

What is the 'correct' way to define and use view helpers in backbone.js?

As far as I can work out, the only real place to define helpers to use in your templates is on the model or collection itself. However, when that helper is directly returning HTML, this begins to feel a little dirty.

Is there a better way?

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

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

发布评论

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

评论(3

醉梦枕江山 2024-12-09 13:30:24

我在 Backbone.js 中放置视图助手的几个不同位置:

如果该助手特定于某个视图,请将其放在视图定义中的右侧:

var MyView = Backbone.View.extend({
  tagName: 'div',

  events: {
    ...
  },

  initialize: function() { ... },

  helperOne: function() {
    // Helper code
  },

  anotherHelper: function() {
    // Helper code
  },

  render: function() { 
    ... this.helperOne() ...
  }
});

如果将使用该助手通过所有视图,扩展 Backbone View 类,以便所有视图继承此功能:

_.extend(Backbone.View.prototype, {
  helper: function() {
    // Helper code
  }
}

如果您需要在视图之间共享更复杂的助手,请让视图相互扩展:

var MyOtherView = MyView.extend({

  // ...

  render: function() { 
    ... this.helperOne() ...
  }
});

我不确定什么是最佳实践(或者是否有既定的最佳实践)实践),但这些模式看起来相当干净并且运行良好。

There are a few different places that I put view helpers with Backbone.js:

If the helper is specific to a certain view, put it right in the view definition:

var MyView = Backbone.View.extend({
  tagName: 'div',

  events: {
    ...
  },

  initialize: function() { ... },

  helperOne: function() {
    // Helper code
  },

  anotherHelper: function() {
    // Helper code
  },

  render: function() { 
    ... this.helperOne() ...
  }
});

If the helper will be used by all views, extend the Backbone View class so that all views inherit this function:

_.extend(Backbone.View.prototype, {
  helper: function() {
    // Helper code
  }
}

If you need more complicated sharing of helpers between views, have views extend each other:

var MyOtherView = MyView.extend({

  // ...

  render: function() { 
    ... this.helperOne() ...
  }
});

I'm not sure what is best practice (or if there is an established best practice), but these patterns seem fairly clean and work well.

若水般的淡然安静女子 2024-12-09 13:30:24

当您构建更大的 Backbone 应用程序时,您可能希望为所有内容命名。然后你就会有一个全球助手的位置。我还没有完成完美的命名空间设置。但现在我正在做这样的事情:

brainswap:{
  appView: {},          <== Reference to instantiated AppView class.
  classes = {           <== Namespace for all custom Backbone classes.
    views : {},
    models : {},
    collections: {},
    controllers : {},
    Router: null
  },
  models: {},          <== Instantiated models.
  controllers: {},     <== Instantiated controllers.
  router: {},          <== Instantiated routers.
  helpers: {},         <== Reusable helper platform methods.
  currentView: {},     <== A reference to the current view so that we can destroy it.
  init: function(){}   <== Bootstrap code to start app.
}

我的视图类看起来像这样:

brainswap.classes.views.profile.contact = brainswap.classes.views.profile.base.extend({...

我的控制器是实例化新视图的对象(并在 currentView 中放置一个引用。记住,您应该始终删除最后一个视图)视图,以便以前的视图事件全部解除绑定,并减少内存使用量。

As you build bigger Backbone apps, you'll probably want to namespace everything. Then you will have a place for global helpers. I haven't made the perfect namespace setup yet. But right now I'm doing something like this:

brainswap:{
  appView: {},          <== Reference to instantiated AppView class.
  classes = {           <== Namespace for all custom Backbone classes.
    views : {},
    models : {},
    collections: {},
    controllers : {},
    Router: null
  },
  models: {},          <== Instantiated models.
  controllers: {},     <== Instantiated controllers.
  router: {},          <== Instantiated routers.
  helpers: {},         <== Reusable helper platform methods.
  currentView: {},     <== A reference to the current view so that we can destroy it.
  init: function(){}   <== Bootstrap code to start app.
}

My view classes look something like this:

brainswap.classes.views.profile.contact = brainswap.classes.views.profile.base.extend({...

My controller is the object that instantiates new views (and places a reference in currentView. Remember you should always remove your last view so the previous views events all get unbinded and your reduce memory usage.

庆幸我还是我 2024-12-09 13:30:24

快速解决方案(CoffeeScript)

Backbone.View::handlebarsTemplate = (templateName) ->
  Handlebars.compile $(templateName).html()

然后您可以在您的视图中使用它:

Yourcoolapp.Views.ThingsIndex extends Backbone.view

  initialize: ->
    @template = this.handlebarsTemplate("#hb_template")
    # etc...

  someMethod: =>
    @template = this.handlebarsTemplate("#hb_other")

Quick solution (CoffeeScript)

Backbone.View::handlebarsTemplate = (templateName) ->
  Handlebars.compile $(templateName).html()

Then you can use that in your view:

Yourcoolapp.Views.ThingsIndex extends Backbone.view

  initialize: ->
    @template = this.handlebarsTemplate("#hb_template")
    # etc...

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