Backbone.js - Given an element, how do I get the view?

后端 未结 5 1822
刺人心
刺人心 2020-12-15 03:33

I\'ve created a bunch of Backbone.js views. Each view has an associated element (view.el).

Given an element on the page — out of context of the view — w

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 04:08

    Since every view has a reference to the model its displaying, what I would do is assign id of the model to the view's associated element(hopefuly that is not affected by the changes by outside event). Also make sure that the model has a reference to its view. Then have these models stored in a Backbone collection.

    With this setup, once something happens to an element, you use the elements id to retrieve corresponding model from Backbone collection that you created above and then this model will give you your view reference.

提交回复
热议问题