简体   繁体   English

在Phonegap / Cordova应用程序中使用Backbone加载HTML本地模板文件

[英]Load HTML local template files with Backbone in a Phonegap/Cordova application

I have this piece of code where I want to load my HTML template from an external files inside a view folder. 我有这段代码,我想从View文件夹中的外部文件加载HTML模板。

var InputView = Backbone.View.extend({
  tagName: 'form',

  template: _.template($.get('views/inputCapo.html')),

  render: function(){
    app.$el.append(this.template);
    return this;
  }

});

app is the main view of my application, I know the issue with the XHR request with local files where I can't load that for security reason. app是我的应用程序的主视图,我知道XHR请求与本地文件有关的问题,出于安全原因,我无法加载该文件。

I know that this is a problem with browsers, but with phonegap application the issue is the same? 我知道这是浏览器的问题,但是使用phonegap应用程序的问题是一样的吗?

Which is the best alternative to accomplish the same feature keeping HTML files separate from scripts? 哪种最佳方法可以实现使HTML文件与脚本分开的相同功能?

I have already saw require.js and text.js library for loading HTML files without $.get but there is again the same dependency on XHR restrictions. 我已经看到require.jstext.js库用于加载没有$.get HTML文件,但是对XHR限制的依赖仍然相同。

It can be much more performant to precompile your templates, rather than requesting them dynamically. 预编译模板,而不是动态地请求模板,可以提高性能。 One way to do this is to use Grunt to build them into a JST namespace - https://github.com/gruntjs/grunt-contrib-jst . 一种方法是使用Grunt将它们构建到JST命名空间中-https: //github.com/gruntjs/grunt-contrib-jst

Then in your Gruntfile, something like this: 然后在您的Gruntfile中,如下所示:

jst: {
  compile: {
    files: {
      "path/to/compiled/templates.js": ["path/to/source/**/*.html"]
    }
  }
}

In your Backbone View: 在您的骨干网视图中:

var InputView = Backbone.View.extend({
  tagName: 'form',

  template: JST['views/inputCapo'],

  render: function(){
    app.$el.append(this.template());
    return this;
  }

});

Then just be sure to include the path/to/compiled/templates.js script in your index.html file before your Backbone views. 然后,只需确保在Backbone视图之前在index.html文件中包含path/to/compiled/templates.js脚本即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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