English 中文(简体)
2. 动态的基调
原标题:backbone view with dynamic id

我刚刚认识到,我误解了<代码>el >属下<代码>Backbone。 View。 基本上,我的意见要求根据模型属性,有活力的<代码>id属性。 我认为,我的工作是出色的,因为我只是在我的模板中明确指出:

<script type="text/template" id="item_template">
  <li class="item" id="{{identifier}}">
    <span class="name">{{name}}</span>
  </li>
</script>

然而,我认识到,“后遗症”实际上是将这一汇编的模板作为另一个要素,即“代码”div。 我阅读了这些文件,了解了这方面的更多情况,但我仍对如何形成动态的<条码>id感到困惑。

最好是,我会想找到一种办法,使上述模板中的缺陷成为我的<代码>el,因为它已经拥有我所希望的一切,但我不知道这是否可行。 因此,我很想知道,简单地说,是否有办法具体指明动态的<代码>id属性。

我尝试在<条码>中确定这一条,首先采用方法,this.id =这一.model.get( attr ),但该条似乎没有任何效果,可能因为目前已经太晚了。

What I m currently doing is just using jQuery to add the id in during render():

this.el.attr(id: this.model.get( identifier ));

当然,我只想问,是否最好通过背后的办法。

最佳回答

Yes there is a standard way to do this in Backbone. You can pass id to the View constructor. You can also refactor your template so that Backbone creates the parent <li> element for you. Try this simpler template:

<script type="text/template" id="item_template">
  <span class="name">{{name}}</span>
</script>

并补充你们的看法:

myView = Backbone.View.extend({
  className: "item",
  tagName: "li"
})

并立即这样做:

var view = new YourView({
  model: mymodel,
  id: mymodel.get( identifier ) // or whatever
})

亲爱!

问题回答

还有一种做法。 我发现,这一条比通过<代码>id更为方便。 你每次都提出看法。

Template:

 <script type="text/template" id="item_template">
      <span class="name">{{name}}</span>
 </script>

View:

var MyView = Backbone.View.extend({
    tagName:  li ,
    attributes: function(){
       return { 
           id: this.model.get( identifier ),
           class:  item //optionally, you could define it statically like before
       }
    }
})

在你提出看法时,通过一名选择,让大家能够找到你现有的预先提交文件的内容:

var id = "1234";
var view = YourView({el:  # +id});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签