[jQuery] Re: Object architecture problem.

2007-12-12 Thread Danny

I actually agree strongly with you: plugins should be chainable. But
not all are (even some jQuery methods are not, like the ones that
return text [val(), html() etc. without arguments]).

On Dec 12, 9:47 pm, "Brian Cherne" <[EMAIL PROTECTED]> wrote:
> I want to disagree with Danny's first statement... if you're going to create
> a jQuery plug-in (re-usable and useful to all) then it's best to follow the
> convention of returning the elements it's acted on. If you want it to return
> something else (or nothing) don't create a plug-in -- instead create a
> separate global object w/methods.
>
> I haven't time to write the innards of the plug-in approach, but I'd
> recommend a syntax like:
>
> $('#photo').photo( method, settings ).css(...).attr(...);
>
> var createSettings = { src:uri };
> var cropSettings = {width:w,height:h};
>
> $('#photo').photo( 'create', createSettings ).photo( 'crop', cropSettings
> ).show();
>
> Chaining is one of the most powerful features of jQuery... let it work for
> you!
>
> Brian.
>
> On 12/12/07, Danny <[EMAIL PROTECTED]> wrote:
>
>
>
> > If you have no reason to chain your plugin (something like $
> > ('#photo').photo(...).css(...).attr(...) then having it return a
> > different object makes sense. But then, why put it in the jQuery
> > namespace at all? 'return new smaon.photo(...) makes as much sense.
> > Danny
>
> > On Dec 12, 10:38 am, Smaon <[EMAIL PROTECTED]> wrote:
> > > Thank you for your answer Danny.
>
> > > I'm currently trying this way:
>
> > > jQuery.fn.photo = function (...) {
> > > var canvas = $(this).get(0);
> > > return new jQuery.photo(canvas, settings);
>
> > > }
>
> > > jQuery.twistMap = function(canvas, settings){
> > >//create code
>
> > >this.crop = function(){
> > >..
> > >   }
>
> > > }
>
> > > usable like this:
>
> > > var photo = $("#photo").photo(...);
> > > photo.crop();
>
> > > What do you think? For me, while not being perfect, it seems more or
> > > less ok...
>
> > > PS: BTW, it there a way to put clean code in this discussion group?


[jQuery] Re: Object architecture problem.

2007-12-12 Thread Brian Cherne
I want to disagree with Danny's first statement... if you're going to create
a jQuery plug-in (re-usable and useful to all) then it's best to follow the
convention of returning the elements it's acted on. If you want it to return
something else (or nothing) don't create a plug-in -- instead create a
separate global object w/methods.

I haven't time to write the innards of the plug-in approach, but I'd
recommend a syntax like:

$('#photo').photo( method, settings ).css(...).attr(...);

var createSettings = { src:uri };
var cropSettings = {width:w,height:h};

$('#photo').photo( 'create', createSettings ).photo( 'crop', cropSettings
).show();

Chaining is one of the most powerful features of jQuery... let it work for
you!

Brian.

On 12/12/07, Danny <[EMAIL PROTECTED]> wrote:
>
>
> If you have no reason to chain your plugin (something like $
> ('#photo').photo(...).css(...).attr(...) then having it return a
> different object makes sense. But then, why put it in the jQuery
> namespace at all? 'return new smaon.photo(...) makes as much sense.
> Danny
>
> On Dec 12, 10:38 am, Smaon <[EMAIL PROTECTED]> wrote:
> > Thank you for your answer Danny.
> >
> > I'm currently trying this way:
> >
> > jQuery.fn.photo = function (...) {
> > var canvas = $(this).get(0);
> > return new jQuery.photo(canvas, settings);
> >
> > }
> >
> > jQuery.twistMap = function(canvas, settings){
> >//create code
> >
> >this.crop = function(){
> >..
> >   }
> >
> > }
> >
> > usable like this:
> >
> > var photo = $("#photo").photo(...);
> > photo.crop();
> >
> > What do you think? For me, while not being perfect, it seems more or
> > less ok...
> >
> > PS: BTW, it there a way to put clean code in this discussion group?
> >
>


[jQuery] Re: Object architecture problem.

2007-12-12 Thread Danny

If you have no reason to chain your plugin (something like $
('#photo').photo(...).css(...).attr(...) then having it return a
different object makes sense. But then, why put it in the jQuery
namespace at all? 'return new smaon.photo(...) makes as much sense.
Danny

On Dec 12, 10:38 am, Smaon <[EMAIL PROTECTED]> wrote:
> Thank you for your answer Danny.
>
> I'm currently trying this way:
>
> jQuery.fn.photo = function (...) {
> var canvas = $(this).get(0);
> return new jQuery.photo(canvas, settings);
>
> }
>
> jQuery.twistMap = function(canvas, settings){
>//create code
>
>this.crop = function(){
>..
>   }
>
> }
>
> usable like this:
>
> var photo = $("#photo").photo(...);
> photo.crop();
>
> What do you think? For me, while not being perfect, it seems more or
> less ok...
>
> PS: BTW, it there a way to put clean code in this discussion group?
>


[jQuery] Re: Object architecture problem.

2007-12-12 Thread Smaon

Thank you for your answer Danny.

I'm currently trying this way:

jQuery.fn.photo = function (...) {
var canvas = $(this).get(0);
return new jQuery.photo(canvas, settings);
}


jQuery.twistMap = function(canvas, settings){
   //create code

   this.crop = function(){
   ..
  }
}


usable like this:

var photo = $("#photo").photo(...);
photo.crop();


What do you think? For me, while not being perfect, it seems more or
less ok...

PS: BTW, it there a way to put clean code in this discussion group?


On 12 déc, 04:47, Danny <[EMAIL PROTECTED]> wrote:
> It sounds like you want a way to create namespaces for plugins. There
> isn't an "official" way, but I wrote a small function to create
> namespaces that was discussed on a previous 
> thread:http://www.nabble.com/Re%3A-passing-this-p13540912s27240.html
>
> Also, it looks like your functions jQuery.Photo.create are actually
> meant to be plugins (i.e. they refer to 'this' as a jQuery object) so
> they should not be part of the jQuery object itself. Just do
> jQuery.fn.extend ({
>photocreate: function(settings){
> 
> },
> photocrop: function(...){
> 
> }
>
> });
>
> Or, if using my namespace plugin,
> $.namespace ('photo', {
>create: function(settings){
> 
> },
> crop: function(...){
> 
> }
>
> });
>
> Danny
> On Dec 11, 6:43 pm, Smaon <[EMAIL PROTECTED]> wrote:
>
> > Hi everybody,
>
> > ok, I'm in a big trouble right now. Let me try to explain my
> > problem :)
>
> > I'm trying to create a simple photo editing system. I need to define a
> > DIV as the canvas, then to have many methods applicables to this
> > object.
>
> > My first reflex was to create a jQuery extension, something like this
> > (just to give the idea):
>
> > jQuery.Photo = {
> > create: function(settings){
> > 
> > }),
> > crop: function(...){
> > 
> > }
>
> > };
>
> > jQuery.fn.extend(
> > {
> > Photo: jQuery.Photo.create,
> > crop: jQuery.Photo.crop
>
> > });
>
> > Very well, now I was able to create my editable photo by using $
> > ("#photo").Photo() and to crop by using $("#photo").crop()
>
> > BUT, the problem is that I need to extend jQuery with every method I
> > want to use on my photos. For example crop() is now in the global
> > scope, and this is very ugly. To do well I should name it PhotoCrop,
> > but this I don't find it's a good solution. What I would like is to
> > have methods only usable on instances of the Photo object, in its
> > local scope. For ex, in core javascript it would be like that:
>
> > function Photo(){
> > ...
>
> > }
>
> > Photo.prototype.crop = function(){
> > ...
>
> > }
>
> > The problem is that I don't find any way to do this properly with
> > jQuery. Maybe somebody here could help me?
>
> > Many thanks in advance!
>
> > Smaon


[jQuery] Re: Object architecture problem.

2007-12-11 Thread Danny

It sounds like you want a way to create namespaces for plugins. There
isn't an "official" way, but I wrote a small function to create
namespaces that was discussed on a previous thread:
http://www.nabble.com/Re%3A-passing-this-p13540912s27240.html

Also, it looks like your functions jQuery.Photo.create are actually
meant to be plugins (i.e. they refer to 'this' as a jQuery object) so
they should not be part of the jQuery object itself. Just do
jQuery.fn.extend ({
   photocreate: function(settings){

},
photocrop: function(...){

}

});

Or, if using my namespace plugin,
$.namespace ('photo', {
   create: function(settings){

},
crop: function(...){

}
});

Danny
On Dec 11, 6:43 pm, Smaon <[EMAIL PROTECTED]> wrote:
> Hi everybody,
>
> ok, I'm in a big trouble right now. Let me try to explain my
> problem :)
>
> I'm trying to create a simple photo editing system. I need to define a
> DIV as the canvas, then to have many methods applicables to this
> object.
>
> My first reflex was to create a jQuery extension, something like this
> (just to give the idea):
>
> jQuery.Photo = {
> create: function(settings){
> 
> }),
> crop: function(...){
> 
> }
>
> };
>
> jQuery.fn.extend(
> {
> Photo: jQuery.Photo.create,
> crop: jQuery.Photo.crop
>
> });
>
> Very well, now I was able to create my editable photo by using $
> ("#photo").Photo() and to crop by using $("#photo").crop()
>
> BUT, the problem is that I need to extend jQuery with every method I
> want to use on my photos. For example crop() is now in the global
> scope, and this is very ugly. To do well I should name it PhotoCrop,
> but this I don't find it's a good solution. What I would like is to
> have methods only usable on instances of the Photo object, in its
> local scope. For ex, in core javascript it would be like that:
>
> function Photo(){
> ...
>
> }
>
> Photo.prototype.crop = function(){
> ...
>
> }
>
> The problem is that I don't find any way to do this properly with
> jQuery. Maybe somebody here could help me?
>
> Many thanks in advance!
>
> Smaon