放在哪里的onload code在RequireJs和骨干应用 [英] Where to put onload code in RequireJs and Backbone App

查看:75
本文介绍了放在哪里的onload code在RequireJs和骨干应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当仁不让的主干应用程序,我使用的是结构有所喜欢这个教程

Doing my first backbone app and I'm using a structure somewhat like this tutorial

我想知道正确的地方,我把我的onload code,如设立的onclick监听器等会是什么?

I'm wondering where the correct place for me to put my onload code, such as setting up onclick listeners etc would be?

我有:

一个简单的引导

require.config({
  paths: {
    jquery: 'libs/jquery/jquery',
    underscore: 'libs/underscore/underscore',
    backbone: 'libs/backbone/backbone'
  }

});

require([

  // Load our app module and pass it to our definition function
  'app',
], function(App){
  // The "app" dependency is passed in as "App"
  App.initialize();
});

的App.js

define(['routers/search'], function(router){
  var initialize = function(){
      this.router = new router();
  } 
  return { initialize: initialize};
});

和然后简单路由器调用中也被定义为调用根据网页上的路由器放在相关的功能的模块路由器relevenent功能

And then a simple router that calls the relevenent function in the router also defined as a module that calls the relevent function on the router depending on the page.

我的感觉是,这个功能在路由器是我应该把我的onload code。这是否正确?

My feeling is that this function in the router is where I should be putting my onload code. Is that correct?

推荐答案

一种可能性是使用RequireJS domready中插件(这可以从他们的短名单的插件下载):的 http://requirejs.org/docs/api.html#pageload

One possibility is to use the RequireJS domReady plugin (it's available for download from their short plugins list): http://requirejs.org/docs/api.html#pageload

下面是他们给出的例子:

Here's the example they give:

require(['domReady'], function (domReady) {
  domReady(function () {
    //This function is called once the DOM is ready.
    //It will be safe to query the DOM and manipulate
    //DOM nodes in this function.
  });
});

所以,那么你可以将其纳入正常的RequireJS结构,知道,无论是DOM加载和任何其他的依赖,你可能已经上市沿着它。

So then you can just incorporate it into your normal RequireJS structure, knowing that both the DOM is loaded plus any additional dependencies you might have listed alongside it.

这篇关于放在哪里的onload code在RequireJs和骨干应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆