Home / برای استفاده از requirejs در کار با کد های backbonejs

برای استفاده از requirejs در کار با کد های backbonejs


بهترین مثالی که دیدم
Model:

define(function (require) {
 
 "use strict";
 
 var $           = require('jquery'),
 Backbone    = require('backbone'),
 
 Employee = Backbone.Model.extend({
 
 
 initialize: function () {
 this.reports = new EmployeeCollection();
 this.reports.url = this.urlRoot + "/" + this.id + "/reports";
 }
 
 }),
 
 EmployeeCollection = Backbone.Collection.extend({
 
 model: Employee,
 
 
 });
 
 return {
 Employee: Employee,
 EmployeeCollection: EmployeeCollection
 };
 
});

View:
define(function (require) {
 
 "use strict";
 
 var $           = require('jquery'),
 _           = require('underscore'),
 Backbone    = require('backbone'),
 tpl         = require('text!tpl/Employee.html'),
 
 template = _.template(tpl);
 
 return Backbone.View.extend({
 
 render: function () {
 this.$el.html(template());
 return this;
 }
 
 });
 
});

define(function (require) {
 
 "use strict";
 
 var $           = require('jquery'),
 Backbone    = require('backbone'),
  
 $content = $("#content");
 
 return Backbone.Router.extend({
 
 routes: {
 "":                 "home",
 "employees/:id":    "employee"
 },
 
 home: function () {
 require(["app/views/Home"], function (HomeView) {
 var view = new HomeView({el: $content});
 view.render();
 });
 },
 
 employee: function (id) {
 require(["app/views/Employee", "app/models"], function (EmployeeView, models) {
 var employee = new models.Employee({id: id});
 employee.fetch({
 success: function (data) {
 var view = new EmployeeView({model: data, el: $content});
 view.render();
 }
 });
 });
 }
 
 });
 
});


فقط کاری که کرده اینه که همه فایل های مورد نیاز همون اول در هر ماژول لود میشن که فلسفه
requirejs مقداری زیر سوال میره
اگر کد رو به جای
مثلا
_           = require('underscore'),
اینشکلی
ShellView =function(){return require(['underscore'],function(){return this})}
یا اینشکلی
ShellView =function(){return require(['underscore'])}
لود کنیم بعد هر بار خواستیم ازش استفاده کنیم به جای
ShellView
بزنیم
ShellView()
بهتر هستش




     RSS of this page