2016-11-15 109 views
0

我已經檢查過有明顯相同問題的人的答案,但我還沒有能夠解決我的問題。骨幹集合不是構造函數

我有呼籲其具有集合(subject.js)集合(levels.js)視圖(teacher.profile.js),但是當我加載頁面的主題集合總是undefined說不是一個構造函數。

刷新很多次後,有時第二個,subject集合在那裏,並工作。

請問誰能告訴我什麼是錯的?

感謝

subject.js

define(["GB"], function(GB) { 

     var subjectModel = GB.Model.extend({ 
     idAttribute:"subjectId", 
     defaults: { 
      subjectId: '', 
      name: '', 
      levelId: '', 
      selected: false 
     } 
     }); 

     return subjectModel; 
    }); 

subjects.js

define([ 
     "GB", 
     "modules/register/teacher/models/subject"], 
     function (GB, SubjectModel) { 

     var subjectCollection = GB.Collection.extend({ 
     model: SubjectModel, 
     url: "webapi/api/Administration/Subject" 
     }); 

     return subjectCollection; 
    }); 

level.js

define(['GB', 'modules/register/teacher/models/subjects'], function (GB, SubjectCollection) { 

     var levelModel = GB.Model.extend({ 
     defaults: { 
      levelId: '', 
      name: '', 
      iconClass: '', 
      subjects: '' 
     }, 
     parse: function(attrs) { 
      **attrs.subjects = new SubjectCollection(attrs.subjects);** //Here is where the error is thrown. SubjectCollection is not a constructor. 
      return attrs; 
     } 
     }); 

     return levelModel; 
    }); 

levels.js

define(["GB", "modules/register/teacher/models/level"], function (GB, LevelModel) { 

     var levelCollection = GB.Collection.extend({ 
     model: LevelModel, 
     url: "webapi/api/Administration/Level" 
     }); 

     return levelCollection; 
    }); 

teacher.profile.js視圖

define([ 
     "GB", 
     "tpl!modules/register/teacher/teacherProfile", "modules/register/teacher/subject.level" 
    ], function(GB, Template, SubjectLevelView) { 
     var view = GB.Views.Item.extend({ 
     template: Template, 
     ui: { 
      subjects: "#subject-list", 
      levels: "#level-list", 
      infoTitle: "#info-title", 
      subjectsLevels: "#subjects-levels" 
     }, 
     initialize: function() { 
      this.userLevels = []; 
     }, 
     onRender: function() { 
      var self = this; 
      this.ui.infoTitle.text(GB.Localise("teacher-data-title")); 
      var levelsPromise = this.collection.fetch(); 
      $.when(levelsPromise) 
      .then(function() { 
       var levelsSubjects = _.map(self.collection.models, function(item) { 
       if (item.get("subjects").length > 0) { 
        var view = new SubjectLevelView({ model: item }); 
        self.userLevels.push(item); 
        return view.render().el; 
       } 
       }); 
       self.ui.subjectsLevels.append(levelsSubjects); 
      }); 
     } 
     }); 
     return view; 
    }); 

main.js

require.config({ 
     map: { 
      '*': { 
       'css': 'plugins/require-css/css', 
       'tpl': 'plugins/require.lodash.template', //this is our templating helper tpl!.html, its brings the template in already underscored, this is faster slightly than text! & subsequent template 
       'lodash': 'underscore' 
      } 
     }, 
     paths: { 
      'plugins': '../plugins', 
      'styles':'../css', 
      'localisation':'core/localisation', 
      'jquery': '../lib/jquery-2.1.4', 
      'jquery.browser': '../plugins/jquery.browser', 
      'jquery.video': '../plugins/vide/jquery.vide', 
      'waypoints': '../plugins/waypoints/jquery.waypoints', 
      'backbone': '../lib/backbone', 
      'marionette': '../lib/backbone.marionette', 
      'text': '../lib/text', 
      'underscore': '../lib/lodash.underscore', //yes underscore is now lodash - its a better performer + extra features + no downside :) 
      'lodash': '../lib/lodash.underscore', 
      'bootstrap': '../lib/bootstrap', 
      'bootstrap-dialog': '../plugins/bootstrap-dialog/js/bootstrap-dialog', 
      'modernizr': '../lib/modernizr-2.8.3', 
      'backbone.validation': '../plugins/backbone-validation', 
      'themepunch.tools': '../plugins/rs-plugin/js/jquery.themepunch.tools.min', 
      'themepunch.rev': '../plugins/rs-plugin/js/jquery.themepunch.revolution.min', 
      'smoothscroll': '../plugins/SmoothScroll', 
      'json': '../plugins/requirejs-plugins/json', 
      'cldr': '../plugins/localisation/cldrjs/cldr', 
      'cldr-data': '../plugins/localisation/cldr-data', 
      'globalize': '../plugins/localisation/globalize/globalize', 
      'localise':'localisation/localise', 
      'GB': 'app' 
     }, 
     shim: { 
      'marionette': { 
       deps: ['backbone'], 
       exports: 'Marionette' 
      }, 
      'backbone': { 
       deps: ['underscore', 'jquery'], 
       exports: 'Backbone' 
      }, 
      'underscore': { 
       exports: '_' 
      }, 
      'backbone.validation': { 
       deps: ['backbone', 'underscore'] 
      }, 
      'bootstrap': { 
       deps: ['jquery'], 
      }, 
      'bootstrap-dialog': { 
       deps: ['bootstrap'], 
      }, 
      'smoothscroll': { 
       deps: ['jquery.browser'] 
      }, 
      'themepunch.tools': { 
       deps: ['jquery'] 
      }, 
      'themepunch.rev': { 
       deps: ['themepunch.tools'] 
      }, 
      'jquery.browser': { 
       deps: ['jquery'] 
      }, 
      'waypoints': { 
       deps: ['jquery'] 
      }, 
      'jquery.video': { 
       deps: ['jquery'] 
      }, 
      'globalize': { 
       deps: ['cldr'] 
      }, 
      'json': { 
       deps: ['text'] 
      } 
     } 
    }); 
    require([ 
     "GB", 
     "routes/application.router", 
     "bootstrap", 
     "core/validation"], 
     function (GB, AppRouter) { 
      GB.routers.application = new AppRouter(); 
      GB.start(); 
     }); 

app.js

define([ 
     "marionette", 
     "core/GB.ini", 
     "globalize", 
     "localisation/localise", 
     "bootstrap-dialog", 
     "json!cldr-data/supplemental/likelySubtags.json", 
     "json!cldr-data/supplemental/plurals.json", 
     "json!cldr-data/supplemental/timeData.json", 
     "json!cldr-data/supplemental/weekData.json", 
     "json!localisation/messages/es.json", 
     "globalize/number", 
     "globalize/message", 
     "globalize/plural", 
     "modernizr", 
     "smoothscroll", 
    ], 
     function (Marionette, AppIni, Globalize, Localise, Dialog, LikeSubtags, Plurals, TimeData, WeekData, Messages) { 
      var GB = window.GB = new Marionette.Application(); 
      GB.User = {}; 
      GB.routers = {}; 

      Globalize.load(
       LikeSubtags, 
       Plurals, 
       TimeData, 
       WeekData 
      ); 
      Globalize.loadMessages(Messages); 
      Globalize.locale("es"); 

      GB.Globalize = Globalize; 
      GB.Localise = Localise; 
      GB.Dialog = Dialog; 

      GB.Model = Backbone.Model.extend({}); 
      GB.Collection = Backbone.Collection.extend({}); 

      GB.Views = { 
       //if we do expand on these views they should probably get their own file. 
       Item: Marionette.ItemView.extend({}), //for a single model 
       Collection: Marionette.CollectionView.extend({}), //for a collection 
       Composite: Marionette.CompositeView.extend({}), //for a combination of a model and a collection 
       Layout: Marionette.LayoutView.extend({}) 
      }; 

      GB.session = new GB.Model(); 

      GB.getUrl = function() { 
       return Backbone.history.location.origin + Backbone.history.location.pathname; 
      } 

      GB.getCurrentRoute = function() { 
       return Backbone.history.fragment; 
      }; 



      GB.on("before:start", function() { 
       var RegionContainer = Marionette.LayoutView.extend({ 
        el: "#app-container", 

        regions: { 
         header: "#header-wrapper", 
         main: "#main-region", 
         footer: "#footer-region", 
         dialog: "#dialog-region" 
        } 
       }); 

       GB.regions = new RegionContainer(); 

      }); 

      GB.on("start", function() { 
       require(["modules/header/header.module"], function() { 
        GB.Header.Controllers.Overview.show(); 
       }); 
       require(["modules/footer/footer.module"], function() { 
        GB.Footer.Controllers.Overview.show(); 
       }); 
       AppIni.start(); 
       Backbone.history.start(); 
       if (GB.getCurrentRoute() === "") 
        Backbone.history.navigate("#home", { trigger: true }); 
      }); 

      return GB; 
     }); 

Foder結構

enter image description here

+0

你能展示一個獲取'json'的例子嗎? – oak

+0

你在這裏,它在老師。配置文件視圖。 GB.session.Levels = new LevelCollection(); var levelsPromise = GB.session.Levels.fetch(); –

+0

這不是提取的'json'。 –

回答

0

好吧,我的第一個猜測是,在levelModel parse方法永遠不會被調用。但它應該始終在fetched上被調用。此外,我重讀你的問題,看到有時它確實對你有用。

所以我現在第二個猜測是race conditionrequirejs。即:有時在levels.js內SubjectCollection是一個真實的Backbone Collection,有時未定義。

這樣一個race的原因之一可以是Circular Dependencies。你能分享完整的源代碼嗎?或者驗證你沒有這樣的circulation

+0

感謝您的回答。我不需要調用解析方法,它在我獲取集合時調用。在視圖中,teacher.profile js文件是我稱之爲Levels集合的獲取的地方。查看我的視圖的呈現。我已經檢查了我從另一篇文章中讀到的循環引用文件,但視圖調用了層次集合和層次集合來創建主題集合。 –

+0

任何想法發生了什麼?我仍然有錯誤,並檢查你告訴我的一切。你需要我分享更多的代碼嗎? Thx –

+0

嘿,如果你可以在線/離線分享代碼,那麼它可以被調試,它肯定會有幫助。 – oak