Backbone.js的&安培; require.js:我怎么改变我的模型,视图和集合到require.js模块? [英] Backbone.js & require.js: how do I transform my models, views and collections to require.js modules?

查看:122
本文介绍了Backbone.js的&安培; require.js:我怎么改变我的模型,视图和集合到require.js模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个JavaScript应用程序与Backbone.js的,这一说,为便于开发构建我的文件,这样

I am working on a javascript app with backbone.js, that said, for ease of development I structure my files this way

app
 |
 + views
 |    L movieRow.js
 |    L movieDetail.js
 |
 + models
 |    L movie.js
 |    L tvshow.js
 |
 + collections
 |    L movies.js
 |    L tvshows.js
 |
 + templates
 |    L movieList.tpl
 |    L movieDetail.tpl
 |    L applicationUI.tpl
 |
 L application.js

现在,这是一个很大的JavaScript文件到服务器的用户,我看到有关labjs或require.js一些嗡嗡声,并决定测试与它周围。但我不知道我怎么会重写我的所有骨干模型或意见纳入这些require.js模块。

now, that are a lot of javascript files to server to the user, I see some buzz about labjs or require.js and decided to test around with it. but i have no idea how i would rewrite all my backbone models or views into these require.js modules.

在正确的方向任何帮助将是很好。

any help in the right direction would be nice.

推荐答案

理解的关键要求(至少在我小的大脑)是要记住,你从函数返回将提供给在拉其他模块(或要求),这个模块。因此,在下面的code,我需要jQuery和骨干网(jQuery是一个特殊的情况下,由规定,jQuery的处理)。当这些2可用它会调用我的函数和函数将返回我创建的视图的构造。

The key to understanding require (at least in my small brain) is to remember that what you return from the function will be available to other modules that pull in (or require) this module. So in the code below, I need jquery and backbone (jquery is a special case handled by "require-jquery"). When those 2 are available it will call my function and that function will return the View constructor that I created.

define([    
    'jquery'
    'libraries/backbone'
], function ($) {
        var MovieRow = Backbone.View.extend({
            render: function () {

                return this;
            }
        });

        return MovieRow;
});

然后,你可能会写这样的事情到您的网页。需要注意的是jQuery是数组中的第一个必需项目。这对应于我的函数的第一个参数。视图是第2和模型是第三名。然后,我可以从使用​​返回值我的定义code以上这是构造。

Then you might write something like this to your page. Note that jquery is the first required item in the array. This corresponds to the first parameter in my function. The View is 2nd and the Model is 3rd. Then I can use the return value from my "define" code above which is the constructor.

require({
    baseUrl: '/'
},[
    'jquery',
    'app/views/movieRow',
    'app/models/movie',
    'libraries/backbone'
], 
function ($, MovieRowView, Movie) {
    var view = new MovieRowView({model : new Movie());
    $('body').append(view.render().el);        
});

希望这是有帮助的。我们已经爱骨干和要求:)

Hope this is helpful... We've been loving Backbone and Require :)

这篇关于Backbone.js的&安培; require.js:我怎么改变我的模型,视图和集合到require.js模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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