简体   繁体   中英

Involve Model's from two different .js

Having -

default.html

<html>
<head>...</head>
<body>
        <script src="Model1.js"></script>
        <script src="Model2.js"></script>
</body>
</html>

Model1.js

...
Model1 = Backbone.View.extend();

Model2.js

...
Model2 = Backbone.View.extend(
    initialize: function(){
    this.model1 = new Model1();
    },
);

it gives an error - Uncaught ReferenceError: Model1 is not defined .

However , all entire in single .html it workout fine -

default.html

<html>
<head>...</head>
<body>
    <script>
    ...
    Model1 = Backbone.View.extend();
     ...
    Model2 = Backbone.View.extend(
        initialize: function(){
        this.model1 = new Model1();
        },
      };
    </script>
</body>
</html>

How could I make it work keeping the 1st page's partition ?

I created a test page just like this and added underscore.js and backbone.js (I assume your page has it).

I get the following error in Chrome developer tools:

Uncaught SyntaxError: Unexpected token : Model2.js:2

This is because your object literal in Model2 is not in curly braces.

Change that code block to this and it works:

Model2 = Backbone.View.extend({
    initialize: function(){
    this.model1 = new Model1();
    },
});

Note that the indentation is a little off. You should indent the contents of the initialize function.

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