D
D
deadkEEper12015-11-14 23:22:23
backbone.js
deadkEEper1, 2015-11-14 23:22:23

Unable to load template via require.js and text plugin. How to do it right and what is wrong?

Using require.js and the text plugin, I try to include the template in the dependency array
(

define(['backbone', 'text!templates/startPageTemplate.html'], function(Backbone, startPageTemplate) {

  var startPageView = Backbone.View.extend({
    el: '#content',
    template: _.template(startPageTemplate),
    initialize: function(){
      this.render()
    },

    render: function(){
    this.$el.html(this.template)
    }

  })

  return startPageView;

})
)
the template itself is simple, say, a test one.
<h1> Hello this is start page </h1>
When I run the code, it gives an error and does not display anything.
XMLHttpRequest cannot load file:///C:/Users/%D0%AE%D1%80%D0%B8%D0%B9/Desktop/requirejs/public/templates/startPageTemplate.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
config
require.config({
  paths: {
    'jquery': 'libs/jquery/dist/jquery',
    'underscore': 'libs/underscore/underscore',
    'backbone': 'libs/backbone/backbone',
    'templates': '../templates',
    'text':  'libs/text/text'

  }
})
require(['views/startPageView'], function(startPageView) {
  new startPageView
})

and html
<!DOCTYPE html>
<html>
<head>
  <title>me app</title>
</head>
<body>
<div id="content"> </div>




<script src="public/js/libs/requirejs/require.js" data-main='public/js/main.js'></script>
</body>
</html>

I'm just getting started with require.js and have no idea what the error is. Thank you)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yaroslav Lyzlov, 2015-11-14
@deadkEEper1

Run a server on localhost and serve everything with it. I understand that you just opened the html file in the browser.

E
Evgeny Petrov, 2015-11-15
@EvgenZZ

here is an example there is a server part https://github.com/jmeas/gistbook on Express e

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question