0

我正在尝试将 JSONStore 独立应用程序集成到我的多页应用程序中。当我尝试初始化集合时,我收到以下错误“Uncaught TypeError: undefined is not a function”。我想知道的一件事是,在 Worklight 6.0 版中,我观察到使用了下划线 (Lo-Dash) 模板。但是我在任何地方都找不到对 lodash 的引用。另外,我在任何地方都没有找到 lodash 文件。谁能告诉我该怎么做?
这是我的javascript代码

窗口.$ = 窗口.jQuery = WLJQ; 当前页面 = {};

currentPage.init = function(WL, jQuery, lodash) { alert("当前页面 ::init 调用");

'use strict';

//Dependencies
var $ = jQuery,
    _ = lodash;

//CONSTANTS
var PEOPLE_COLLECTION_NAME = 'people',
    KEY_VALUE_COLLECTION_NAME = 'keyvalue',
    INIT_FIRST_MSG = 'PERSISTENT_STORE_NOT_OPEN',
    NAME_FIELD_EMPTY_MSG = 'Name field is empty',
    AGE_FIELD_EMPTY_MSG = 'Age field is empty',
    ID_FIELD_EMPTY_MSG = 'Id field is empty',
    EMPTY_TABLE_MSG = 'No documents found',
    DESTROY_MSG = 'Destroy finished succesfully',
    INIT_MSG = 'Collection initialized',
    ADD_MSG = 'Data added to the collection',
    REPLACE_MSG = 'Document replaced succesfully, call find.',
    REMOVE_MSG = 'Documents removed: ',
    COUNT_MSG = 'Documents in the collection: ',
    CLOSE_ALL_MSG = 'JSONStore closed',
    REMOVE_COLLECTION_MSG = 'Removed all data in the collection',
    LOAD_MSG = 'New documents loaded from adapter: ',
    PUSH_MSG_FAILED = 'Could not push some docs, res: ',
    PUSH_MSG = 'Push finished',
    PASS_CHANGED_MSG = 'Password changed succesfully'; 

$('#init').click(initCollection);      
$('#destroy').click(destroy);    
$('#add-data').click(addData);
$('#find-name').click(findByName);  
$('#find-age').click(findByAge);
$('#find-all').click(findAll);
$('#find-id-btn').click(findById); 
$('#replace').click(replace);
$('#remove-id-btn').click(removeById);

//Log messages to the console and status field
var _logMessage = function (msg, id) {
    //Get reference to the status field
    var status = _.isUndefined(id) ? $('div#status-field') : $(id);

    //Put message in the status div
    status.text(msg);

    //Log message to the console
    WL.Logger.info(msg);
};

//Show JSONStore document in a table
var _showTable = function (arr) {

    if (_.isArray(arr) && arr.length < 1) {
        return _logMessage(EMPTY_TABLE_MSG);
    }

    //Log to the console
    WL.Logger.ctx({stringify: true, pretty: true}).info(arr);

    var
        //Get reference to the status field
        status = $('div#status-field'),

        //Table HTML template
        table = [
            '<table id="user_table" >',
                '<tr>',
                    '<td><b>_id</b></td>',
                    '<td><b>name</b></td>',
                    '<td><b>age</b></td>',
                    '<td><b>json</b></td>',
                '</tr>',
                '<% _.each(people, function(person) { %>',
                    '<tr>',
                        '<td> <%= person._id %> </td>',
                        '<td> <%= person.json.name %> </td>',
                        '<td><%= person.json.age %></td>',
                        '<td><%= JSON.stringify(person.json) %></td>',
                    '</tr>',
                '<% }); %>',
            '</table>'
        ].join(''),

        //Populate the HTML template with content
        html = _.template(table, {people : arr});

    //Put the generated HTML table into the DOM
    status.html(html);
};


//Scroll to the top every time a button is clicked
$('button').on('click', function () {
    $('html, body').animate({scrollTop: 0}, 'slow');
});



function initCollection() {
     console.log("init collection method called");
     alert("init collection method called");
    //Get references to the input fields DOM elements
        var usernameField = $('input#init-username'),
            passwordField = $('input#init-password');

        //Get values from the input fields
        var username = usernameField.val() || '',
            password = passwordField.val() || '';

        //Create the optional options object passed to init
        var options = {};

        //Check if a username was passed
        if (username.length > 0) {
            options.username = username;
        }

        //If if a password was passed
        if (password.length > 0) {
            options.password = password;
        }

        //JSONStore collections metadata
        var collections = {};

        //Define the 'people' collection and list the search fields
        collections[PEOPLE_COLLECTION_NAME] = {

            searchFields : {name: 'string', age: 'integer'},

            //-- Start optional adapter metadata
            adapter : {
                name: 'people',
                add: 'addPerson',
                remove: 'removePerson',
                replace: 'replacePerson',
                load: {
                    procedure: 'getPeople',
                    params: [],
                    key: 'peopleList'
                }
            }
            //-- End optional adapter metadata
        };

        //Define the 'keyvalue' collection and use additional search fields
        collections[KEY_VALUE_COLLECTION_NAME] = {
            searchFields : {},
            additionalSearchFields : { key: 'string' }
        };

        //Initialize the people collection
        WL.JSONStore.init(collections, options)

        .then(function () {
            _logMessage(INIT_MSG);
            _callEnhanceToAddKeyValueMethods();
        })

        .fail(function (errorObject) {
            _logMessage(errorObject.msg);
        });
}

提前致谢

关于
VHC

4

1 回答 1

1

我观察到使用了下划线(loadash)模板。但是我在任何地方都找不到对 loadash 的引用。另外,我在任何地方都没有找到 loadash 文件。谁能告诉我该怎么做?

您可以构建自己的lodash版本或使用underscore

worklight 使用的 lodash 版本在WL_变量中。查看您的代码,也许您想替换_ = lodash_ = WL_. 或者,当您使用自己的版本 lodash 或下划线时,它将自动分配给_变量。

或者,您可以通过修改字符串原型来使用其他字符串模板库,如Handlebars.js基本字符串插值。

于 2013-11-07T19:54:19.637 回答