【问题标题】:What does this BlackBerry Web App Javascript code do?这个 BlackBerry Web App Javascript 代码有什么作用?
【发布时间】:2013-11-22 10:48:27
【问题描述】:

我熟悉几种编程语言,但在理解 Javascript 以及它在移动应用程序中的使用方式方面遇到了困难。我正在为 BlackBerry 开发并使用 BlackBerry 10 jQuery Mobile Theme。我正在查看示例中的 App.js,并且对 App 对象是什么感到困惑。

App = {};

App.init = function () {
    console.log("App Init");
    App.utils.metaHack();
    $("#activity").live("pageinit", function(){
        App.page.activity.init();
    });
    $("#bb_activity").live("pageinit", function(){
        App.page.bb_activity.init();
    });
    $("#progressPage").live("pageinit", function(){
        App.page.progress.init();
    });
    $("#sliderPage, #sliderPageDark").live("pageinit", function(){
        App.page.slider.init();
    });
    $("#togglePage, #togglePageDark").live("pageinit", function(){
        App.page.toggle.init();
    });
    $("#actionBarSample").live("pageinit", function() {
        App.page.actionBarSample.init();
    });
    $('#applicationMenu').live("pageinit", function() {
        App.page.applicationMenu.init();
    });
}

App.utils = {
    metaHack: function () {
        var meta = document.createElement("meta");
        meta.setAttribute('name','viewport');
        meta.setAttribute('content','initial-scale='+ (1/window.devicePixelRatio) + ',user-scalable=no');
        document.getElementsByTagName('head')[0].appendChild(meta);
    }
}

App.page = {
    activity: {},
    bb_activity: {},
    progress: {},
    slider: {},
    toggle: {},
    actionBarSample: {},
    applicationMenu: {}
}

App.page.activity.init = function() {
    $('#show').on('click', function () {
            $.mobile.loading('show');
    });
    $('#text').on('click', function() {
        $.mobile.loading('show', {
            text: 'Loading',
            textVisible: true,
            textonly: true,
            theme: 'a'
        });
    });
    $('#swatch-a').on('click', function() {
        $.mobile.loading( 'show', {
            text: 'Loading',
            textVisible: true,
            theme: 'a'
        });
    });
    $('#swatch-a-notext').on('click', function() {
        $.mobile.loading( 'show', {
            theme: 'a'
        });
    });
    $('#swatch-c').on('click', function() {
        $.mobile.loading( 'show', {
            text: 'Loading',
            textVisible: true,
            theme: 'c'
        });
    });
    $('#swatch-c-notext').on('click', function() {
        $.mobile.loading( 'show', {
            theme: 'c'
        });
    });
    $('#hide').on('click', function () {
        $.mobile.loading('hide');
    });
}

App.page.bb_activity.init = function() {
    console.log("bb_activity");
    $('#throttle').on('change', function () {
        console.log("throttle");
        var speed = $('#throttle').val();
        $('#speedTest').activityindicator('speed', speed+'s');
    });
}

App.page.progress.init = function() {
    var p = 0;
    var error = pause = false;

    function watchProgress() {
        if( p > 100 || error || pause) {
            return;
        }
        $('#rogress').progressbar("setValue", p);
        p+= 4;
        setTimeout(watchProgress, 100);
    }


    $('#start').on('vclick', function () {
        error = false;
        watchProgress();
    });

    $('#error').on('vclick', function () {
        $('#rogress').progressbar("setError", error = !error);
    });

    $('#pause').on('vclick', function () {
        $('#rogress').progressbar("pause", pause = !pause);
    });

    $('#reset').on('vclick', function () {
        p = 0;
        error = pause = false;
        $('#rogress').progressbar("setValue", p);
    });
}

App.page.slider.init = function() {
    $('#slider-disabled').slider('disable');
    $('#slider-disabled-highlight').slider('disable');
}

App.page.toggle.init = function() {
    console.log("toggle init");
    $('#flip-disabled').slider('disable');
}

App.page.actionBarSample.init = function() {

    var $tabo = $("#tover"),
    overflowState = $tabo.hasClass("noContent");

    $("#left").on("panelbeforeopen", function() {
        //Save the state of the overflow button
        overflowState = $tabo.hasClass("noContent");
        $tabo.addClass("noContent");
    })
    .on("panelbeforeclose", function() {
        //Put the overflow button into the correct state
        if(!overflowState) {
            $tabo.removeClass("noContent");
        }
    });

    //Handle overflow menu clicks
    $(".bb10-panel").bind("vclick", function() {
        //Close the panel
        $(this).panel("close");
    });

    $("#left li").bind("vclick", function() {
        //Clear the active state from any other buttons that may have been set to active
        $(this).siblings().removeClass("ui-btn-active");
        //Add the active state to the selected button
        $(this).addClass("ui-btn-active");
        //Clear the contents of the tab overflow button
        //Add class to put the tab overflow icon in the correct position
        //Clear the active state from all tab buttons in action bar
        $('[data-role="tab"], [data-role="tab-overflow"]').removeClass("active");
    });

    $(".inBar").bind("vclick", function() {
        //Set the active state to the tab in the actionbar
        $('#' + this.id.slice(0, 2)).addClass("active");
        $tabo.addClass("noContent").empty();
        overflowState = true;
    });

    $(".notInBar").bind("vclick", function() {
        //Set the active state to the tab overflow button
        $tabo.empty()
        .addClass("active")
        .html('<img src="img/generic_81_81_placeholder.png" alt=""><p>' + $(this).text() + '</p>')
        .removeClass("noContent");
        overflowState = false;
    });

    $("[data-role='tab']").bind("vclick", function() {
        //Change page on tab click
        if($(this).data("href")) {
            $.mobile.changePage( $(this).data("href"), { transition: "slideup"} );
        }
    });
}

App.page.applicationMenu.init = function() {
    if(typeof blackberry != 'undefined'){
        blackberry.event.addEventListener('swipedown', function(){
            $('#top').panel("open");
        });
        $('#menuBtn').css('display','none');
    }
    else{
        $('#simulInst').css('display','none');
    }
}

App.init();

App 是黑莓特有的对象吗?我做了一些涉猎,做了一个小应用程序,但没有使用 App 或初始化任何东西。

【问题讨论】:

  • 我对黑莓一无所知,但您提供的代码是标准的东西:在全局命名空间中创建一个对象,然后将所有属性和方法放在那里,而不是污染全局命名空间。在您提供的代码中,它甚至在最后调用App.init(),所以可能没有魔法。
  • 它只是一个通用对象吗? init() 定义在哪里?
  • App.init() 定义在第三行:App.init = function () {...

标签: javascript mobile blackberry


【解决方案1】:

本例中的 App 在顶部定义: App = {};

所以它只是一个新的普通旧 JavaScript 对象(字典),然后他们为它定义函数和数据,例如App.utils = ....

您可以在浏览器上试用,按 F12 然后转到控制台 (ESC) 并键入例如blah = {},您将看到一个名为 blah 的新对象。显然,JavaScript 中的一切都是对象。

了解更多

【讨论】:

  • 非常感谢。 App.utils = function utils(){} 啊我现在明白了。定义函数的新方法让我感到困惑。
  • @RapsFan1981,几乎正确,例如App.init = function () { 将定义一个您可以调用的函数,例如 App.init();。在上面的代码中,App.utils.metaHack() 是在名为 utils 的字典中创建的。
  • 哦,我的肉是 JavaScript 中的 App.utils = Java 中的函数 utils(){} 对吧?
  • @RapsFan1981,嗯,在 Javascript 中有两种定义函数的方法,或者像 function myFunction() {} 这样独立,或者附加到变量或对象,例如myFunction = function(){}.
猜你喜欢
  • 2011-10-12
  • 1970-01-01
  • 2011-05-09
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多