0

Handelbars.js は初めてなので、試してみたいと思いました。handlebars.js でレンダリングしたい html ファイルが 1 つだけあります。

HTML ファイル:

<html>
<head>
    <script type="text/javascript">
        console.log("Head:getting css");
    </script>
    <link rel="stylesheet" type="text/css" href="css/main_2.css">
</head>

<body>
    <div class="phone-screen">
        <script type="text/javascript">
            console.log("Body:start MasterPage");
        </script>
        <!--MasterPage -->
        <script id="app-bar" type="text/x-handlebars-template">
            <div class="app-bar-container">
                </div>
                <div class="nav-bar-container">
                    <div class="nav-tab-left nav-tab-selected nav-tab-label-Selected">
                        <div class="tab-lable-centered ">History</div>
                    </div>
                    <div class="nav-tab-right nav-tab-notSelected ">
                        <div class="tab-lable-centered ">New</div>
                    </div>      
                </div>
        </script


        <!-- Content -->
        <script id="workout-list-tpl" type="text/x-handlebars-template">
            <div class="list-container">
                <div class="week-seperator">
                    <div class="week-lable-right ">Week 12</div>
                </div>
                <div class="workout-card-white">
                    <div class="workout-card-label ">Workout</div>
            </div>
        </script>

        <script type="text/javascript">
            console.log("Body:getting Scripts________________________");
        </script>
        <script src="lib/handlebars.js"></script>
        <script src="js/storage/memory-store.js"></script>
        <script src="lib/jquery-1.8.2.min.js"></script>
        <script src="js/MainView.js"></script>
        <!--<script src="js/EmployeeView.js"></script>-->
        <script src="js/main_2.js"></script>
    </div>
</body>

アプリの JavaScript:

var app = {
            initialize: function() {
     console.log("initializer start: ");
     var self = this;
     //this.detailsURL = /^#employees\/(\d{1,})/;
     //this.registerEvents();
     this.store = new MemoryStore(function() {
        self.route();
     });
        },

        route: function() {
     console.log("route function start.");
     var self = this;
     var hash = window.location.hash;
     if (!hash) {
        if (this.homePage) {
            //this.slidePage(this.homePage);
        } else {
            console.log("homePage: ");
            this.homePage = new MainView(this.store).render();
            console.log("homePage = " + this.homePage );
            //this.slidePage(this.homePage);
        }
        return;
     }
     var match = hash.match(this.detailsURL);
     if (match) {
        //this.store.findById(Number(match[1]), function(employee) {
            //self.slidePage(new EmployeeView(employee).render());
        //});
     }
        },

        showAlert: function (message, title) {
     if (navigator.notification) {
        navigator.notification.alert(message, null, title, 'OK');
     } else {
        alert(title ? (title + ": " + message) : message);
     }
        },

    };
    console.log("app.initialize();");
    app.initialize();

そしてview.js:

var MainView = function(store) {

this.render = function() {
    console.log("render function execution:" + MainView.template() );
    this.el.html(MainView.template());
    return this;
};

this.initialize = function() {
    console.log("MainView init start.");
            // Define a div wrapper for the view. The div wrapper is used to attach events.
           this.el = $('<div/>');
           //this.el.on('keyup', '.search-key', this.findByName);
    };

    this.initialize();

}
console.log("Handlebars.compile($(app-bar).html()); " );
MainView.template = Handlebars.compile($("#app-bar").html());

コンソール ログ:

Head:getting css main_2.html:4
Body:start MasterPage main_2.html:12
Body:getting Scripts________________________ main_2.html:41
Handlebars.compile($(app-bar).html());  MainView.js:19
app.initialize(); main_2.js:46
initializer start:  main_2.js:5
route function start. main_2.js:15
homePage:  main_2.js:22
MainView init start. MainView.js:10
render function execution:
            <div class="app-bar-container">
                </div>
                <div class="nav-bar-container">
                    <div class="nav-tab-left nav-tab-selected                      nav-tab-label-Selected">
                        <div class="tab-lable-centered ">History</div>
                    </div>
                    <div class="nav-tab-right nav-tab-notSelected ">
                        <div class="tab-lable-centered ">New</div>
                    </div>      
                </div>
         MainView.js:4
 homePage = [object Object] 

コードは、html コードを取得する view.js の render 関数に渡されますが、次に何が起こるのか、ブラウザでレンダリングされない理由がわからないのです。

4

1 に答える 1

0

生成された html 文字列を html ページに追加する最後の手順を忘れていたようです。

$('body').append(page.el);
于 2013-11-15T13:26:41.323 に答える