473,756 Members | 7,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

prototype.js object persistance

I've created a new class using prototype.js. After I make the
ajax.request all references to this.myClassMet hodorVariable are lost.
Does the ajax method blow out the object persistance? I'm fairly new to
OOP javascript so could be (and probably am) overlooking some detail.
Below is the logic of what i'm trying to do.

//Javascript code
var myClass = Class.create();
myClass.prototy pe={
initialize: function(fm) {
//Static Variables
this.myMessage = 'hello';
this.myName = 'Nick';
this.frm = fm;
this.WSURL = '/some/Server/script';
this.frm.onSubm it =
this.submitForm .bindAsEventLis tener(this);
},
submitForm: function(evt) {
var pars = myMessage;
var myAjax = new Ajax.Request( this.WSURL, { method: 'get',
parameters: pars, onSuccess: this.myHandler} );
},
myHandler: function(ajaxRe sponse) {
this.myMessage( ajaxResponse.re sponseText);
},
myMessage: function(str) {
alert(str);
}
};

//HTML Code
<form name="someForm" id="someForm" method="post" action="##">
<input type="text" id="myText" />
<input type="submit" name="submit" value="submit">
</form>
<script>var testClass = new myClass('somefo rm');</script>

May 25 '06
45 3032

Ian Collins wrote:

Expect to see it more as the popularity of Ruby on Rails grows. This
framework is tied closely to Prototype.js.


I've been thinking about this too because I've been using Rails for
about 10 months and it seems like a solid framework. I like programming
with Rails and I have not encountered bugs. However the choice to use
Prototype.js as the core JavaScript library seems like it was a very
bad choice according to many here. Does this reflect on other choices
the Rails core team made or is this one of only a few poor choices?

Peter

May 27 '06 #11
pe**********@gm ail.com said the following on 5/26/2006 10:13 PM:
Randy Webb wrote:
The same can be said for eval.
eval itself may or may not be evil, its just the person using it, right?
Well, anybody that understands the problems/drawbacks of eval enough to
know when to use doesn't use it.
That last sentence seems to mean the the knowledgable don't ever use
eval.


That isn't what I meant. It means they don't use unless there is a
reason to use it. eval's purpose is to execute code unknown at runtime.
The problem with eval is people using it as a crutch such as:

eval('document. getElementById( "someVar'+someO therVar+'").stl ye.visibility') ;

instead of:

document.getEle mentById('someV ar' + someOtherVar).s tyle.visibility ;

Or, the worst one lately was referencing global variables. Assume you
have 10 variables named myVar## and you want to get a reference to them
dynamically. How do you do it?

Well, you could do eval('myVar' + ##) but it's using a crutch when
window['myVar' + ##] does the same thing, does it quicker, and does it
without starting up the eval compiler.

And yes, I use it when it is needed. What is the best/quickest way to
convert a fraction to a decimal using JS? The answer depends on the
browser but some browsers do it quicker with eval than splitting the
fraction and dividing.
Doesn't JSON absolutely require the use of at least one eval?
Not sure, I very seldom use JSON.
I don't mean this to be nit picking.
And I didn't take it that way :)
I'm curious if there is another way to deal with a JSON response
or if you think those using JSON should be doing something else?


It depends on the JSON response. If all it is is data, you could create
a script element, insert the data, and off you go.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 27 '06 #12
pe**********@gm ail.com wrote:
Randy Webb wrote:
The same can be said for eval.
eval itself may or may not be evil, its just the person using it, right?
Well, anybody that understands the problems/drawbacks of eval enough to
know when to use doesn't use it.

That last sentence seems to mean the the knowledgable don't ever use
eval. Doesn't JSON absolutely require the use of at least one eval? I
don't mean this to be nit picking. I'm curious if there is another way
to deal with a JSON response or if you think those using JSON should be
doing something else?

I don't think there is another way, a JSON response is the textual
representation of an object which has to be converted into a live object.

--
Ian Collins.
May 27 '06 #13
Ian Collins <ia******@hotma il.com> writes:
I don't think there is another way, a JSON response is the textual
representation of an object which has to be converted into a live object.


You could parse it directly. The grammar is simple enough.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
May 27 '06 #14
Lasse Reichstein Nielsen wrote:
Ian Collins <ia******@hotma il.com> writes:

I don't think there is another way, a JSON response is the textual
representatio n of an object which has to be converted into a live object.

You could parse it directly. The grammar is simple enough.

True, but what advantage does that offer over eval?

--
Ian Collins.
May 27 '06 #15
Ian Collins <ia******@hotma il.com> writes:
Lasse Reichstein Nielsen wrote:
Ian Collins <ia******@hotma il.com> writes:
I don't think there is another way, a JSON response is the textual
representati on of an object which has to be converted into a live object.


You could parse it directly. The grammar is simple enough.

True, but what advantage does that offer over eval?


None, if you trust the sender. But it *is* another way than using eval.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
May 27 '06 #16
pe**********@gm ail.com wrote:
Randy Webb wrote:
The same can be said for eval.
eval itself may or may not be evil, its just the person
using it, right? Well, anybody that understands the
problems/drawbacks of eval enough to know when to use
doesn't use it.
That last sentence seems to mean the the knowledgable don't
ever use eval.


99.9% of the uses of eval that you see are cases where the use of eval
is not necessary and the alternatives are objectively superior
(particularly, but not exclusively, regarding performance). The
knowledgeable javascript author does not use eval when is not
*necessary*. He/she also will tend to follow the axiom; if you think you
need to use eval the odds are very good that you are missing a much
better alternative. So when I say necessary I mean really thought about
and known to be absolutely the only way, or offering sufficient
advantages in context to compensate for its drawbacks and outweigh the
alternatives.
Doesn't JSON absolutely require the use of at least one eval?
Absolutely not. If JSON comes in through an XML HTTP request from a
trusted source (you own server side code) then eval probably is the
optimum mechanism for turning that into a javascript object structure.
JSON may come in in other ways, such as loaded into a hidden IFRAME
having been wrapped into a SCRIPT element on an HTML page, and JSON that
is passed on from a third party must never be evaled, rather actively
parsed by the client, else you are open to (potentially malicious)
script insertion along the lines of:-

{
prop:((function (){
/*

This is the body of a function expression that will be _called_ during
the eval-ing of the containing (apparent) JSON data and will do
something that you do not want to happen, such as change every link on
the current page to go to porn sites, or every input field to broadcast
whatever is entered to a third party, and so on.

*/
})())
}
I don't mean this to be nit picking. I'm curious if there is
another way to deal with a JSON response or if you think those
using JSON should be doing something else?


JSON is probably the most efficient data format for sending information
to client-side javascript, and eval is the most efficient (and widely
supported) way of turning a string of JSON data directly into object
with which javascript can interact. That does not mean that the people
using JSON do not have to put some thought into what they are doing.

Richard.
May 27 '06 #17
pe**********@gm ail.com wrote:
Ian Collins wrote:

Expect to see it more as the popularity of Ruby on Rails
grows. This framework is tied closely to Prototype.js.
I've been thinking about this too because I've been using
Rails for about 10 months and it seems like a solid
framework. I like programming with Rails and I have not
encountered bugs.


How hard have you been trying to break it? We know that it will not
function with IE <= 5.5 (unless the JScript DLLs have been updated) and
that there are issues with current Opera versions. The odds are also
good that it will fall over if exposed to any less dynamic browser.

It is not difficult to write an IE only script and then never encounter
any issues with it, by the simple measure of never testing it with
anything but default installation of IE 6. (Which makes it particularly
embarrassing for Microsoft that MSDN repeatedly kicks out
'"parentElement .parentElement" is null or not an object' errors when
visited with a default instalation of IE 6 :)

(It is also interesting to note that Prototype.js's issues with JScript
< 5.6 mostly relate to the absence of Function.protot ype.call and
Function.protot ype.apply, both of which can be successfully emulated. So
a little feature detection and prototype augmentation can fix that
issue, where augmenting the prototype of Function is significantly less
problematic than augmenting the prototypes of Object or Array.)
However the choice to use Prototype.js as the core JavaScript
library seems like it was a very bad choice according to many
here.
Prototype.js appears to be client side code written by people with a
limited familiarity with javascript (such that, for example, the
contents of ECMA 262 where apparently unknown to them) and no real
appreciation of or love for the language anyway. There are attempts to
make javascript look and behave like another language, which obviously
will appeal to individuals familiar with that other language but is
almost certainly not going to be the best use of javascript.
Does this reflect on other choices the Rails core team made or
is this one of only a few poor choices?


It is not realistic to expect experts in one language to be in a
position to make good judgements about another. Part of the appeal of
client-side libraries is that one can be identified as apparently suited
to a task and the responsibility for the quality of the code it contains
abdicated to the author(s) of that library. Many people fall into that
(potential) trap in all innocence.

In any design for a combined server side-client side framework the
interface between the two should be as simple as possible, and probably
should be abstracted of the details of the languages on both sides. In
that case the choice of client side code does not matter much as it can
be regarded as provisional and better replacements found/created.

If server side and client side get too intertwined (as might be manifest
in, for example, the server sending actual javascript code (specific
function calls and the like, not merely data in JSON format) to be
evaled on the client) then the lack of flexibility in the entire system
is a poor design choice.

If Rails cannot work without Protoype.js then the issues with
Protyope.js will harm Rails. The vested interests my not want to see
that, but that doesn't mean it could not be true.

On the other hand, there is the question of what Rails is a framework
for. If it is for creating Internet applications with a restricted and
known UA base (so not for public commercial web use) then maybe the
issues with Prototype.js don't matter. The system is sufficient for its
role and being unsuited to other roles just means that it should not be
used in those contexts.

Richard.
May 27 '06 #18
JRS: In article <4d************ *@individual.ne t>, dated Sat, 27 May
2006 14:45:31 remote, seen in news:comp.lang. javascript, Ian Collins
<ia******@hotma il.com> posted :
I don't think there is another way, a JSON response is the textual
representati on of an object which has to be converted into a live object.


Below, S is, I suppose, the textual representation of an object; it
appears that Ob ends up as a live object.

S = "{X:3}"
Fn = new Function("retur n "+S)
Ob = Fn()
alert(Ob.X)

And
function Obj(St) { return new Function("retur n "+St)() }

I'm not claiming any great advantage (though such may exist) other than
that of not exciting eval-hunters.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 28 '06 #19
Well, people ranted and bashed but nobody cared to answer the question.
It seems that you're having trouble with the "this" keyword. Rule of
thumb: avoid "this" inside anonymous functions and also in functions
you pass around as references.
Change your submitForm method to:
submitForm: function(evt) {
var pars = myMessage;
var myAjax = new Ajax.Request( this.WSURL, { method: 'get',
parameters: pars, onSuccess: this.myHandler. bind(this)});
},

OR

submitForm: function(evt) {
var pars = myMessage;
var myObj = this;
var myAjax = new Ajax.Request( this.WSURL, { method: 'get',
parameters: pars, onSuccess: function(){ myObj.myHandler (arguments); }
} );
},

If I didn't goofed up something (not rare), one of the above options
should work for you.

I hope this helps.

- Sergio
http://www.sergiopereira.com/articles/

May 28 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
980
by: Michael.McD | last post by:
Is there any consensus on the way to go when implementing object persistance to a dB? For example MHibernate v. DataObjects (x-tensive). Cheers, Michael McD
7
1257
by: Florian Loitsch | last post by:
hi, in section 10.1.8 (Arguments Object) it is stated that the "internal ] property of the arguments object is the orginal Object prototype object, the one that is the *initial* value of Object.prototype". Furthermore the Object.prototype property has attributes . My question now: what does the "initial" refer to? To the untouched prototype-object, or to the current Object.prototype? In the latter case: why would the write "initial", if...
8
3759
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the bind function. The problem with Javascript is that the "this" operator is poorly overloaded and it is often hard to understand in the context of object-oriented javascript So, let's start with the definition:
8
2061
by: Robert | last post by:
Hi, I can use "with" like this: function MyObject(message) { this.message = message; } function _MyObject_speak() {
2
3036
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass' priviledged methods also. Do you know if this is possible ? In the following example, I create an ObjectA (variable a), an ObjectB which inherits ObjectA (variable b) and an ObjectC which inherits ObjectA (variable c1). The 'toString ()' method...
4
1770
by: lkrubner | last post by:
I'm reading an essay, I think one of Crockford's, and it has this example in it: function Demo() { } Demo.prototype = new Ancestor(); Demo.prototype.foo = function () { } ; Does Ancestor now have a function called foo? What if I have 5 different objects, all descended from Ancestor? Do
13
2572
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){}, my_meth2: function(){} }); to define new methods on the MyObj prototype object. Object.extend
5
2242
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
3
3586
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: Ajax.Base = function() {}; Ajax.Base.prototype = { setOptions: function(options) { <...>
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.