【问题标题】:exporting and use the backbone model and view thow error导出和使用主干模型和视图抛出错误
【发布时间】:2023-12-31 23:29:01
【问题描述】:

我在我的应用程序中使用“require.js”,我收到此错误:

ReferenceError: Can't find variable: appModel
require.js:32Error: Load timeout for modules: listView

我无法理解我这边出了什么问题:

我的配置是:

requirejs.config({
    baseUrl:"scripts",
    paths:{
        //libraries
        jquery          :"lib/jquery-1.9.0.min",
        jqueryUI        :"lib/jquery-ui-1.9.2.custom.min",
        underScore      :"lib/lodash-1.0.0.min",
        backBone        :"lib/backbone-min",
        //scripts
        appInit         :"js/tasklist",
        loginValidate   :"js/loginValidate",
        listModel       :"js/models/listModel",
        listCollection  :"js/collections/listCollection",
        listView        :"js/views/listView"
    },
    shim:{
        "underScore":{
            exports: "_"
        },
        "backBone":{
            exports:"Backbone",
            deps:["underScore"]
        },
        "appInit" : {
            deps:["jquery","jqueryUI","underScore","backBone"]
        },
        "jqueryUI":{
            deps:["jquery"]
        },
        "loginValidate":{
            deps:['jquery']
        },
        "listModel":{
            exports:"listModel",
            deps:["backBone"]
        },
        "listCollection":{
            exports:"listCollection",
            deps:["listModel"]
        },
        "listView":{
            exports:"listView",
            deps:["listModel","listCollection"]
        }
    }
});

require(['jquery'],function(){
    if($('#tlLoginForm').length){
        require(["jquery","loginValidate"],function($){
            $(function(){
                var paramsLoginForm = {
                        loginForm : $('#tlLoginForm')
                    };
                var validate = tasklistHandler(paramsLoginForm);
                validate.init(); //it works fine.
            });
        });
    }
    if($("#boardTemplate").length){ // i am finding my template and initiating the values
        require(["backBone","listModel","listCollection","listView"], function(){

        });
    };
});

listModel.js 中我的模型

require(['jquery','backBone'],function($,Backbone){ // i am importing jquery, and backbone and assigning the model
var appModel = Backbone.Model.extend({
        url : 'data/data.json',
        defaults:{
          "id"                  :"id",
          "title"               :"Title",
          "projectName"         :"project",
          "dueDays"             :0,
          "dueTime"             :0,
          "dueDate"             :"0-0-0000",
          "totalTasks"          :0,
          "taskCompleted"       :0,
          "percent"             :65,
          "taskStatus"          :"Assigned",
          "jobtype"             :"vip",
          "username"            :"scott.pierce@groupfmg.com",
          "notes"               :"notes1"
        }  
      });

      return appModel; // tried not works
});

我在 listCollection.js 中的收藏

define(["jquery","backBone","listModel"],function($,Backbone,model){ 

// 我正在导入我在 listModel.js 中声明的 jquery、主干和模型,但它不是 工作! console.log('collection',model); // 我的模型未定义 var collection = Backbone.Collection.extend({ 型号:型号, 初始化:函数(){ 控制台.log(this.model); } });

    var newModel = new appModel; //ReferenceError: Can't find variable: appModel
    console.log(newModel); // throw the error as like i mentioned.
});

谁能帮我解决我的问题?

【问题讨论】:

  • listModel.js 应该在声明后返回appModel,并且所有paths 都正确吗?尤其是listModellistCollectionlistView ?
  • 都对了,我把appModel还给我。
  • 我检查了所有内容都是正确的,我在控制台中没有收到任何 404 错误
  • appModel 将在 listCollection.js 中以 model 的形式提供,因为您在定义回调中以 model 的形式接收它。
  • 我已经尝试过这样,但它给出“未定义”

标签: backbone.js requirejs


【解决方案1】:

这对我有用..

我没有使用"require",而是使用了"define"。现在一切正常。

谢谢大家。

【讨论】:

    最近更新 更多