0

I'm starting on a Backbone AMD application, using React.js for the views. The view is not recognizing the JSX I've written in the render() function. Am I missing a statement in the router or view? The error I'm receiving is 'Unexpected token <'.

Below is the app flow from top to bottom:

Index.html

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<!--<script src="http://fb.me/react-0.12.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.12.0.js"></script> -->
<script data-main="js/main" src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.1.15/require.min.js"></script>

main.js

require.config({
paths: {
   jquery: 'http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min',
   underscore: 'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min',
   backbone: 'http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min',
   react: 'http://fb.me/react-0.12.0',
   JSXTransformer: 'http://fb.me/JSXTransformer-0.12.0'
}
});

require([

 // Load our app module and pass it to our definition function
'app',
], function(App){
// The "app" dependency is passed in as "App"
App.initialize();
});

app.js

define([
 'jquery',
 'underscore',
 'backbone',
 'router' // Request router.js

], function($, _, Backbone, Router){
   var initialize = function(){
   // Pass in our Router module and call it's initialize function
   Router.initialize();
}

return {
  initialize: initialize
};
});

router.js

define([
'jquery',
'underscore',
'backbone',
'views/team/list' 
 ], function($, _, Backbone, TeamListView){

 var AppRouter = Backbone.Router.extend({
   routes: {
   // Define some URL routes
  'team': 'showTeam',

  // Default
  '*actions': 'defaultAction'
  }
});

 var initialize = function(){
    var app_router = new AppRouter;

    app_router.on('route:showTeam', function(){
    // Call render on the module we loaded in via the dependency array
    // 'views/teams/list'
    var teamListView = new TeamListView();
    teamListView.render();

  });

  app_router.on('route:defaultAction', function(actions){
    // We have no matching route, lets just log what the URL was
    console.log('No route:', actions);
  });

  Backbone.history.start();

};
return {
  initialize: initialize
};
});

list.js

/**
 * @jsx React.DOM
*/
define(
  [
  'jquery',
  'underscore',
  'backbone',
  'react',
  'JSXTransformer'
  ], function($, _, Backbone, react, JSXTransformer){

    var MyWidget = React.createClass({
      handleClick: function() {
        alert('Hello!');
      },
      render: function() {
        return (
          <a href="#" onClick={this.handleClick}>Do something!</a>
          );
       }
    }),

    var TeamListView = Backbone.View.extend({
      el: 'body',
      template: '<div class="widget-container"></div>',
      render: function() {
        this.$el.html(this.template);
        React.renderComponent(new MyWidget(), this.$('.widget-container').get(0));
        return this;
      }
    });
  // Our module now returns our view
  return TeamListView;
});

list.js (updated - working version)

define(
  [
  'jquery',
  'underscore',
  'backbone',
  'react'
  ], function($, _, Backbone, React){

  var MyWidget = React.createClass({displayName: 'MyWidget',
      handleClick: function() {
      alert('Hello!');
  },
  render: function() {
    return (<div>
      <ul>
      <li></li>
      <li></li>
      </ul>
      <div>Test</div>
      <a href="#" onClick={this.handleClick}>Do something!</a>
      </div>
      )
  }
});

var TeamListView = Backbone.View.extend({

  el: $('#mainContent'),
        events: {

        },

        initialize: function() {
            console.log('test');
        },

        render: function (){

          React.render(
            React.createElement(MyWidget, null),
            document.getElementById('mainContent')
          );
        }     
  });

  return TeamListView;
});
2
  • Try a fork of require-jsx to combine JSX and Require. Commented Nov 2, 2014 at 17:52
  • This answer might help you: Using ReactJS with RequireJS Commented Nov 3, 2014 at 7:36

2 Answers 2

1

JSX must be compiled to JavaScript before browsers can understand it.

You can use the jsx tool to do this.

JSX transformer and Require.js are incompatible.

Sign up to request clarification or add additional context in comments.

3 Comments

Do I have to separate the JSX components into separate files? The parser is throwing an error on the list.js file: Error while reading module list: Error: Parse Error: Line 27: Unexpected token var
The error is occurring on the 'var TeamListView = ...' @FakeRainBrigand
Nevermind, I got it now. See answer for new version of list.js
1

If you're using RequireJS, you can use a JSX plugin like this one. For what it's worth, when I integrated the JSX transformer into our RequireJS setup, it required quite a bit of tweaking and experimentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.