jQuery插件如何维护对象状态 [英] jquery plugin how to maintain state of object

查看:91
本文介绍了jQuery插件如何维护对象状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的插件,但是我遇到了如何管理插件状态的问题.

I am trying to create a simple plugin , and I've faced with problem on how plugin's state should be managed.

(function ($) {
// Static things for plugin goes here
var uiHtml = "<div class='gaw-box'>" +
      "</div>";



var methods = {


    init: function (options) {

        return this.each(function () {
            // Create UI
            $(this).html(uiHtml);

            if (options) {

                var defaults = {
                    name:"N/A"
                };

                var opt = $.extend(defaults, options);
                  $(this).find(".gaw-name").html(opt.name);
            }

            // Visual Events attach
            var uiobj = $(this).find(".gaw-box");
            $(uiobj).mouseenter(function () {
                if (!this.isSelected) {
                    $(this).css('border', '1px solid red');
                }
            });

            $(uiobj).mouseleave(function () {
                if (!this.isSelected) {
                    $(this).css('border', '1px solid black');
                }
            });

            $(uiobj).click(function () {
                this.isSelected = !this.isSelected;
                if (this.isSelected) {
                    $(this).css('border', '3px solid red');
                }
                else {
                    $(this).css('border', '1px solid black');
                }

            });

            });

    },
    getIsSelected: function (options) {

        return this.isSelected; // ALWAYS FALSE
    },
    destroy: function () { }
};

$.fn.gateaway = function (method) {
    var plugin = this;

    plugin.isSelected = false;

    if (methods[method])
    {
        return methods[method].apply(plugin, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method) {
        return methods.init.apply(plugin, arguments);
    } else {
        $.error('Method ' + method + ' does not exist on jQuery.pluginName');
    }
};
})(jQuery);

我要实现的目的是保存插件(对象)的状态(如果没有选择). 我叫

What I am trying to achieve is to save state of the plugin (object) , if it's selected for example or not. I am calling my plugin like

$("#gate").gateaway('getIsSelected')

结果始终为false ...我知道问题出在"this"范围内,这是我第一次在客户端上开发该问题,第二次我需要在今天完成它:-),因此,如果可以指出我在哪里或如何组织该插件以能够保存每个插件的状态,它将为我节省:-)

the result is always , false ... I know that the problem is with "this" scope , the problem that this is first time I am developing on client , and the second i need to finish it today :-) , so if it's possible to point me where or how can i organise the plugin to be able save state of each plugin it will save me :-)

推荐答案

看看有关在插件中存储状态的官方jQuery文档: http://docs.jquery.com/Plugins/Authoring#Data

Have a look at the official jQuery docs about storing state in plugins: http://docs.jquery.com/Plugins/Authoring#Data

这篇关于jQuery插件如何维护对象状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆