与生态模板Backbone

与生态模板Backbone

本文介绍了与生态模板Backbone.js的:如何包括在模板中的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

是否有可能包括在模板中的模板?也许类似ERB处理谐音的方式的东西吗?

Is it possible to include a template within a template? Maybe something similar to the way ERB handles partials?

,而不是试图渲染嵌套模型像ERB一种时尚,这是更好地让Backbone.js的照顾这。

Rather than attempting to render nested models in a fashion like ERB, it's better to let Backbone.js take care of this.

请注意,我用的CoffeeScript的语法:

Note, I am using coffeescript syntax:

Projects.IndexView

Projects.IndexView

template: JST["backbone/templates/projects/index"]

addAll: () ->
    @options.projects.each(@addOne)

addOne: (project) ->
    view = new Worktimer.Views.Projects.ProjectView({model : project})
    @$("#projects-table").append(view.render().el)

render: ->
    $(@el).html(@template(projects: @options.projects.toJSON() ))
    @addAll()

示范项目有一个嵌套的集合称为会话:

the model Project has a nested collection called sessions:

Projects.ProjectView

Projects.ProjectView

template: JST["backbone/templates/projects/project"]

$(@el).html(@template(@model.toJSON() ))
for s in @model.sessions.models
    v = new Worktimer.Views.ProjectSessions.ShowView(model: s)
    $(@el).find('.sessions').append(v.render().el)

ProjectSessions.ShowView

ProjectSessions.ShowView

template: JST["backbone/templates/project_sessions/show"]

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

那么,我们到底有嵌套模板是这样的:

so, in the end we have nested templates like this:


  • 项目总览

    • 项目

      • 会话

      • 会话

      • 会话

      • 会话


      • 会话


      • 会话

      • 会话

      推荐答案

      这里的小帮手我用脊椎:

      here a little helper I use for spine:

      # Render Partials in ECO-Templates like in Rails-ERB
      #
      # usefull to clean up structure in spine.js and other js-mvc´s like backbone
      #
      # usage:
      #   <%- render_partial 'path/to/partial' %>  ..  will render ../spine-app/views/path/to/_partial.jst.eco
      #   <%- render_partial 'path/to/partial', foo: 'bar' %>  ..  will render ../spine-app/views/path/to/_partial.jst.eco  ..  locals = @foo
      #
      window.render_partial = ( path, options = {} ) ->
          # add the leading underscore (like rails-partials)
          path = path.split('/')
          path[ path.length - 1 ] = '_' + path[ path.length - 1 ]
          path = path.join('/')
          # render and return the partial if existing
          try
              JST["app/views/#{ path }"]( options )
          catch error
              # if App.Environment != 'production' then "<p class='error'>Sorry, there is no partial named '#{ path }'.</p>" else ''
              "<p class='error'>Sorry, there is no partial named '#{ path }'.</p>"
      

      这篇关于与生态模板Backbone.js的:如何包括在模板中的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

      1403页,肝出来的..

09-06 13:49