简体   繁体   中英

Having trouble moving a existing Model file into Require.js

I have moved the project into require now I am attempting to re-write some the code using backbone and underscore. The traditional model, view , controller files exist. Below Is the start of an attempt to move the Model into a require define.

define ("Model", function () {

   var Model = function (connectToServer, soundTypeNum, isMobile)
   {
       //code
   };
return Model;

   // };//Error

  });//Fixed

But I am getting the following error:

Uncaught SyntaxError: Unexpected end of input

EDIT: Now I am Getting Model is undefined when controller tries to create a new instance of Model:

var model = new Model(connectToServer);

Any ideas?

I believe require.js must take an array of dependencies, even if there's only one. Also make sure you're passing the dependencies to the function, as Nit said.

define(["Model"], function(theModel) {
    var Model = // initialize Model here
    return Model;
});

If you're not initiating Model somewhere else, then why is it a dependency? You can pass a blank array of dependencies to require's function, if need be:

define([], function() {
    var Model = // initialize Model here
    return Model;
});

I'm not sure how you're planning on bringing backbone into play later "to make code more manageable". I would think it would be easier to declare Model as a backbone model in this file where you initiate it. Just define backbone as a dependency and extend its Model class.

define(["underscore", "backbone"], function(_, Backbone) {
    var Model = Backbone.Model.extend({
        // initialize Model here.
    });
    return Model;
});

Hope this helps.

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