/    Sign up×
Community /Pin to ProfileBookmark

Connect encapsulated code with a jquery plugin

Hello everyone, I have only programmed old-school javascript and I’m not used to the way is it used nowadays (unobtrusive, encapsulated, using objects for everything, etc). So please bear with me if this question seems silly.

This is what I need:

I’m using a jQuery plugin called Flexigrid to create grids to list items. I’m not going to get into this. Just mention that I have configured these buttons in it (notice the callbacks):

[code=php]$(document).ready(function() {
$(‘#listado’).flexigrid({
//propiedades
…..
buttons : [
{name: ‘Add’, bclass: ‘add’, onpress : item.add()},
{name: ‘Edit’, bclass: ‘edit’, onpress : item.edit()},
{name: ‘Delete’, bclass: ‘delete’, onpress : item.del()},
{separator: true}
]
});
} [/code]

On the other hand, I’d like to know whether I can define the [B]onpress[/B] callbacks so that they get encapsulated. Something like this:

[code=php](function($) {
var item = {
add: function() {
alert(‘add item’);
},
edit: function() {
alert(‘edit item’);
},
del: function() {
alert(‘delete item’);
}
}
})(jQuery); [/code]

Logically, the callbacks will not work because [B]item[/B] is encapsulated. How would you do this to prevent collisions? You might say join both snippets within the same closure. Problem is that I need the first one to be output from a template because it depends on PHP variables, whereas the second one is in a js file.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Jazztronik spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...