简体   繁体   English

如何创建使用另一个jQuery插件的jQuery插件?

[英]How to create a jQuery plugin which uses another jQuery plugin?

I'm trying to make my first jQuery plugin. 我正在尝试制作我的第一个jQuery插件。 Basically it's just like a wrapper to a goMap map which updates some latitude and longitude text fields whenever someone moves the marker on a google map. 基本上,这就像goMap地图的包装,每当有人在Google地图上移动标记时,它都会更新一些纬度和经度文本字段。

I have it set up like this (this is going to look pretty rough because I wrote it in coffeescript and am just translating as I write the question so ignore any missing semi-colons or whatever please!): 我有这样的设置(这看起来很粗糙,因为我用coffeescript编写了它,并且在我编写问题时只是翻译,所以请忽略任何遗漏的分号或其他任何内容!):

(function($) {
  $.fn.correctionMap = function(options) {
    defaults = {
      latFieldSelector: "#listing_latitude"
      longFieldSelector: "#listing_longitude"
      resetLinkSelector: "#reset_marker_link"
      marker_id: 'listing_marker'
    }

    options = $.extend(defaults, options);

    // grab the text field elements
    latField = $(options.latFieldSelector)
    longField = $(options.longFieldSelector)

    // inialise the map go-ordinates from the
    // values in the text fields
    initialLat = Number(latField.val())
    initialLong = Number(longField.val())

    // build the options object for the goMap plugin
    map_options = {
      longitude: initialLong
      latitude: initialLat
      zoom: 16
      maptype: 'ROADMAP'

      markers: [{
        longitude: initialLong
        latitude: initialLat
        draggable: true
        id: options.marker_id
      }]
    };

    // add the map to the div
    return this.goMap(map_options)
)(jQuery)

Then when I call it with $("#correction_map").correctionMap() it doesn't seem to do anything. 然后,当我用$("#correction_map").correctionMap()调用它时,它似乎什么也没做。 Yet when I console.log this.goMap(map_options) from inside the plugin, it looks like it is getting initialized fine (cursory inspection). 但是,当我从插件内部进行console.log this.goMap(map_options) ,看起来好像初始化得很好(定期检查)。 Am I doing the return wrong or something? 我在做退货错误吗?

Turns out you have to give the div which is the target for the map a height and width with CSS when you use goMap. 事实证明,使用goMap时,必须使用CSS为div赋予地图的目标高度和宽度。 The Javascript in my question is fine. 我的问题中的JavaScript很好。 Oops! 糟糕!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM