简体   繁体   中英

Require.js model collection fetch difficulty

Okay I'm going to do my best to explain this.

I am using require.js. I have a file named ScheduleView.js and it loads Subject.js .

Subject.js

define([
    'jquery',
    'underscore',
    'backbone',
    'libs/myFunctions/schedule/collections/SubjectList'
], function($, _, Backbone, SubjectList){
    var Subject = Backbone.Model.extend({
        initialize: function(){
            subjectList = new SubjectList;
            subjectList.fetch({
                success: function(){
                    console.log('yay! fetched');
                }
            })

...

return Subject;

The Subject model is used ALOT. And I can't make it fetch the data every time. That's just a waste of performance. It's also too much of a hassle to initialize Subject once in the ScheduleView and pass on the variable;

Are there other alternatives of doing this?

Hope you understand the problem. Thanks

Aha! Inside 'libs/myFunctions/schedule/collections/SubjectList' i simply wrote:

var subjectList = new Subject
return subjectList

That way I could simple include the link every time I needed it with only having it fetched the information once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM