【问题标题】:Loading templates using jQueryMobile with Marionette使用 jQueryMobile 和 Marionette 加载模板
【发布时间】:2013-05-27 16:41:31
【问题描述】:

我正在尝试将 JQM 与 Marionette 一起使用,但遇到了一个问题。

问题:我有一个 index.html 页面,其中包含空 div,其中 data-role="page" 和 "content"。我的下划线模板文件只有一个 JQM 样式的标签和按钮。

到目前为止,我已经成功地使用 Marionette.ItemView 和 Marionette 区域加载了我的模板。一切都运行良好,例如加载所有必需的脚本,如 jQuery、JQM、Backbone.Marionette,加载 jQueryMobile css 等。视图以及标签和按钮都成功渲染。

但问题是我在模板中为 div 设置的主题没有反映,甚至按钮或标签 UI 也不是 JQM 提供的。

我认为在加载模板后我应该刷新页面。但是我应该在我的代码中在哪里以及如何做到这一点。我尝试了很多代码,但没有任何效果。

这是 index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Index</title>
<!-- data-main attribute tells require.js to load config.js after require.js loads. -->
<script data-main="../js/apps/config" src="../js/ext_libs/require.js"></script>
</head>
<body>
<div data-role="page" data-theme="a">

    <header id="header" data-role="header"></header>

    <div id="main" class="container-fluid" data-role="content"></div>

    <footer data-role="footer" class="footer">
    </footer>

</div>
</body>
</html>

这是我的 index_view

define([ "marionette", "templates" ], function( Marionette, templates ) {
console.log("Success..Inside Index View.");
var IndexView = Marionette.ItemView.extend({
    template : templates.index_body({title: 'Worrrrrld', btn_submit: 'Submit'})
});
return IndexView;
});

还有模板

<div data-theme="b" id="index_view">
<label><%= title %></label>
<input type="submit" value=<%= btn_submit %> />
</div>

我的 App.js 文件

define([ "jquery", 
     "underscore", 
     "backbone", 
     "marionette", 
     "index_view", 
     "header_view" ], function($,_,
    Backbone, Marionette, IndexView, HeaderView) {
'use strict';
console.log("Success...Inside App");
console.log("Success..Libraries loaded successfully.");

var app = new Marionette.Application();
console.log("Success..App Object Got Created.");

app.addRegions({
    header : '#header',
    main : '#main',
    footer : '#footer'
});
console.log("Success..Regions Added");

app.addInitializer(function() {
    console.log("Success..Creating Header View Object.");
    app.header.show(new HeaderView());
    console.log("Success..Creating Index View Object.");
    app.main.show(new IndexView());
});

app.on('initialize:after', function() {
    Backbone.history.start();
});

$(document).on('pagebeforeshow', '#index', function(){
    console.log("Success..I am triggering event");
    $('[data-role="content"]').trigger('create');
});

console.log("Success..Returning App Object.");
return app;
});

【问题讨论】:

  • 感谢您的回复..但是请您建议我应该在哪里放置代码 $(document).on('pagebeforeshow', '#index', function(){ // 增强新选择元素 $('[data-role="content"]').trigger('create'); });
  • @Gajotres 我尝试将其放入我的 app.js 脚本中,如上所示。但是没有用,请告诉我我是使用 Jquery 和 Marionette 编程的新手

标签: jquery jquery-mobile backbone.js marionette


【解决方案1】:

@Nithalia

你可以看看jquerymobile-marionette。在那里,为了对 JQM 页面进行建模,使用了 Backbone.Marionette.Layout。您可以看到模板是如何加载和呈现的。

【讨论】:

  • 感谢您的回复.. 但我已经提到了那个链接.. 不同之处在于我没有使用 JQM 路由器,除了一切都一样但仍然无法正常工作......请告诉我一些其他选项。
  • 你能发布你的 config.js 吗?同样在您的 index_view 中,它会加载来自哪里的“模板”?
  • 你好 Ming.. 正如你所建议的,我提到了你指定的代码。它工作完美,但你能告诉我 helper.js 中使用的方法是正确的主干代码吗..它不完全是木偶……它是低级别的。我尝试实现布局,但现在之前可见的模板,现在它们消失了......我已经在这个位置发布了我的完整代码,请检查并告诉我......尽快
  • 刚刚分叉了你的。对您的代码进行了一些小的更改并发布了一个拉动
猜你喜欢
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多