初始化主干视图时

发布于 2024-12-22 13:45:06 字数 109 浏览 1 评论 0原文

如何才能在每次初始化backbone.js 视图时运行一个函数?

我正在寻找可以放在正常视图代码之外的东西,作为backbone.js 的扩展。

这个想法是减少样板文件的数量。

How do I make it so that a function runs every time a backbone.js view is initialized?

I'm looking for something that I can put on outside of my normal view code, as an extension to backbone.js.

The idea is to reduce the amount of boilerplate.

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

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

发布评论

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

评论(3

囚你心 2024-12-29 13:45:06

由于 Javascript 不是真正的面向对象编程语言,因此您不能像 java 或 c# 那样使用继承来解决问题。

一种可能的解决方案是使用工厂设计模式。

您可以调用工厂方法来实例化视图,而不是直接实例化视图。

var viewFactory = function(view, viewOptions) {
  //perform your boilerplate code
  return new view(viewOptions);
}

AView = Backbone.View.extend({});

var person = new Backbone.Model({name: 'Paul'});
var view = viewFactory(AView, { model: person });

这是一个 jsfiddle 示例

它不是其他语言可能的优雅解决方案,但它确实工作。

Since Javascript is not a true object oriented programing language, you can't use inheritance to solve your problem as you could if it was java or c#.

One possible solution is to use the factory design pattern.

Instead of instantiating your view directly, you can call a factory method that will instantiate your view.

var viewFactory = function(view, viewOptions) {
  //perform your boilerplate code
  return new view(viewOptions);
}

AView = Backbone.View.extend({});

var person = new Backbone.Model({name: 'Paul'});
var view = viewFactory(AView, { model: person });

Here's a jsfiddle example

It's not as an elegant solution that is possible with other languages, but it does the job.

以为你会在 2024-12-29 13:45:06

使用内置的backbone.js初始化函数:

http://documentcloud.github.com/backbone/#视图构造函数

var ItemView = Backbone.View.extend({
  initialize: function(){
    alert('View Initialized');
  }
});

编辑:我应该更清楚。

用 Patrick Ewing 的话说,这里http://podcast.rubyonrails.org/programs /1/episodes/railsconf-2007:

“如果它像鸭子一样走路并且像鸭子一样说话,那么它就是一只鸭子,对吗?所以如果这只鸭子没有给你你想要的噪音,你必须打那只鸭子,直到它返回你所期望的“

Duck Punch(或 Monkey Patch,如果你愿意)Backbone 对象。

Backbone.View.prototype.initialize = function(){
  alert('I overrode the default initialize function!');
}

use the builtin backbone.js initialize function:

http://documentcloud.github.com/backbone/#View-constructor

var ItemView = Backbone.View.extend({
  initialize: function(){
    alert('View Initialized');
  }
});

EDIT: I should be more clear.

In the words of Patrick Ewing found here http://podcast.rubyonrails.org/programs/1/episodes/railsconf-2007:

"if it walks like a duck and talks like a duck, it’s a duck, right? So if this duck is not giving you the noise that you want, you’ve got to just punch that duck until it returns what you expect"

Duck Punch (or Monkey Patch if you prefer) the Backbone object.

Backbone.View.prototype.initialize = function(){
  alert('I overrode the default initialize function!');
}
娇女薄笑 2024-12-29 13:45:06

您可以使用 Backbone.Events。

在应用程序的顶层或全局对象上:

app.eventManager = {};
_.extend(app.eventManager, Backbone.Events);
app.eventManager.bind("newView", app.yourfunction(view));

在您想要触发函数的任何视图的初始化方法中:

app.eventManager.trigger("newView", this);

其中“this”是作为“view”参数传递给函数的视图实例。

You can use Backbone.Events.

On the top level of your app or on the global object:

app.eventManager = {};
_.extend(app.eventManager, Backbone.Events);
app.eventManager.bind("newView", app.yourfunction(view));

And in the initialize method of any view you want to trigger your function:

app.eventManager.trigger("newView", this);

where "this" is the view instance passed as the "view" parameter to your function.

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