Jump to content

MrFish

Members
  • Posts

    492
  • Joined

  • Last visited

Everything posted by MrFish

  1. Here is my problem. I have a class called a DefinitionMap which will act as a Controlling class. It will have definitions for fields like- name = "[first_name] [last_name]" which will parse to "John Smith". The actual definitions will be stored in an object called "definitions". This class needs to use prototype and extend a class called StandardObject but that's irrelevant here (but I do need to use prototype for this). Here is my basic DefinitionMap class. function DefinitionMap(){}DefinitionMap.prototype.definitions = {};// ModifiersDefinitionMap.prototype.define = function(field, definition){ this.definitions[field] = definition;}// AccessorsDefinitionMap.prototype.get = function(field){return this.definitions[field];} The problem with this is that definitions is already defined and all Objects constructed from this class will share the definitions object unless it's redefined within the objects own scope. Here is an example of what I mean- http://jsfiddle.net/4JGMf/1/ You can see here that Definition1 defines "test_field" but Definition2 automatically has access to it. One way to get around this without any forethought would be to treat the DefinitionMap(){} function as the constructor and define definitions in it. This works until you create a subclass on the DefinitionMap. I will eventually want to create preset definitions for different Model classes. function DefinitionMap(){ this.definitions = {};}DefinitionMap.prototype.definitions = null; http://jsfiddle.net/pvTju/1/ This can all be solved (with hassle) by creating an init method and call that method whenever a new object is constructed on one of these classes. You can try to call the init class automatically in the DefinitionMap constructor but this still doesn't work for subclasses. function DefinitionMap(){ this.init();}DefinitionMap.prototype.definitions = null;// ModifiersDefinitionMap.prototype.init = function(){ this.definitions = {}; } Will work be reluctant to use- Definition1 = new PersonDefinition();Definition1.init(); // it hurts to liveDefinition2 = new PersonDefinition();Definition2.init(); // please kill me http://jsfiddle.net/R77wP/1/ The first test makes sense because the definitions variable is assigned to an object created in the window scope. The second also makes sense because the subclass prototype creates a new object on the parent class once so any reference to the parent classes variables will point to one instance of the class. Any practical work-arounds for this?
  2. Ok. That answers my question. So If I send a redirect to the browser with a 301 status it should know that the page has moved and not count it as 2 unique pages. I'll need to check with our SEO guys but that makes sense to me.
  3. I'm working on a site with an alias system that will redirect the browser to an aliased page if the non-aliased page is requested. So if you go to mysite.com/about.php it will redirect to mysite.com/about. I want to do this so google doesn't consider them 2 different pages (which it is now). Here is the redirect code- header("HTTP/1.1 301 Moved Permanently");header("location: $newURL");exit(); I thought if I changed the header to a 301 header google would know this was a mistake and the new url was the actual page. However this is not the case. So my question is- Does this header information ever get passed back to the browser? Does the browser do the redirect or does PHP redirect (and the browser just updates the address bar)? Do I need to change the status on the resulting page to 301 instead of 200?
  4. Because I have a lot of xml to parse and to make it simpler I wanted to create a function that would run through an element recursively and create variables without any other hassle. $valueSheet = <<<TXTcom_status=[Status]com_priceLow=[PriceLow]com_priceHigh=[PriceHigh]com_sqftLow=[SqftLow]com_sqftHigh=[SqftHigh]com_number=SubdivisionNumbercom_name=SubdivisionNamecom_description=SubdivisionDescriptioncom_street1=SubAddress->SubStreet1com_city=SubAddress->Citycom_state=SubAddress->Statecom_lat=SubAddress->SubGeocode->SubLatitudecom_lng=SubAddress->SubGeocode->SubLongitudecom_directions=DrivingDirectionsTXT; getValues($Subdivision, $valueSheet); echo $com_name; // Some Community The best I can do is return an array and use extract on that. It's not a road block and 9 extra characters won't kill me I just wanted to see if I could do it like extract is doing it. But I'm guessing I can't replicate that functionality in php.
  5. Well I was trying to get around using this foreach loop everytime I called the function and was hoping I could write a function like the extract function. http://php.net/manua...ion.extract.php But I couldn't think of any way to match the functionality exactly. $arr = array("test"=>1,"test2"=>2,"test3"=>3); function extract2($arr){ foreach($arr as $key => $val) { $$key = $val; }} function extract3($arr){ foreach($arr as $key => $val) { $GLOBALS[$key] = $val; }} extract2($arr);echo $test; // obviously this wouldn't work because the scope of the variables are in the function extract3($arr);echo $GLOBALS["test"]; // works- yes- but not what I intended If that makes sense. EDIT: How extract works now- $arr = array("test"=>1,"test2"=>2,"test3"=>3);extract($arr); echo $test; // prints 1
  6. Is it possible to recreate php's extract function? I have a custom implementation I'd like to write. I really just need the change the scope of variables created in a function the without using the GLOBALS array.
  7. I've got a file upload form that allows you to upload PDF files. The form contains 2 hidden fields- one is a table and one is an id for the row I'm updating. The form works fine and everything goes smoothly until I got to around 8-9MB PDFs- at which point not only does the file not upload but no fields are posted. I figured this would be an issue with the upload_max_filesize and more likely the post_max_size since it's more than just the file not coming through. When I went to make a change in the htaccess here is what I found- php_value post_max_size 100Mphp_value upload_max_filesize 15M The posted form does not exceed either of these limits and even put upload_max_filesize at 100M to be safe. Still- if the file is too big it will not post even the hidden fields. I'm pretty stumped- any ideas on troubleshooting this?
  8. I'd like to use those features as well as the pin features. The idea is that these custom maps can either be placed over the real map or just floating in empty space but behave exactly the same. It looks like I can change the styles of everything in google maps to straight white. That might be what I'm looking for.
  9. Is it possible to use the Google Map Framework for a custom-drawn map of a specific area? I can overlay the area over map but several hundred of these custom drawn maps don't have geo-coordinates set to them. So I just want the custom-drawn map to float out in white empty space unless it has geo-coordinates set. I could overlay a white box over the entire globe but that's more of a workaround and I was looking for something less hack-ish.
  10. valueOf will work for me. I can just give my class a valueOf method. As long as it works the same with strings and integers it should be fine.
  11. Is it possible to give an object a _toComparison value and have that automatically used when comparing 2 objects? For example: c = new Calculation();c.multiply(10, 10); c2 = new Calculation()c2.divide(1000, 10); if(c == c2) // 100 == 100{ // do stuff} This would be really helpful in my current situation where I'm not trying to compare if the object is the same as another but some contents of each. The function can be comparing simple data types as well as objects so I'm trying to keep for checking the type before comparing.
  12. PHP doesn't support the ws prototcal natively. That's why.
  13. I don't have much experience with sockets in general but I'd like to learn how to use these websockets. I will use PHP to setup the server end- though I know PHP is not the most suited for this sort of thing. Instead of using a WebSocket class for PHP I'd like to do it from scratch just to see how it works. The below code is mostly copied-pasted from a tutorial I was looking at- <?php set_time_limit(0); $port = 9011; $address = "localhost"; $sock = socket_create(AF_INET, SOCK_STREAM, 0); socket_bind($sock, $address, $port) or die("Could not bind socket on $port"); socket_listen($sock); echo "listening...\n"; $client = socket_accept($sock); $input = socket_read($client, 1024); $output = ereg_replace("", $input).char(0); socket_write($client, $output); socket_close($client);?> # php -q socket_server.phplistening... And the JavaScript looks like this- connection = new WebSocket('ws://mysite.net:9011/tests/socket_server.php'); connection.onopen = function() { console.log("Connection open!"); } connection.onclose = function() { console.log("Connection closed"); } connection.onerror = function(e) { console.log("socket error: " + e); } When I go to this page to test it (on the same server as the php) I get "Connection closed" in the console and nothing else. This happens a few seconds after the page loads. I've opened up port 9011 on the server for all incoming and outgoing TCP/UDP connections. Anyone know what I could be doing wrong?
  14. Ah ok. But I need to make a custom function for this don't I? I'll have a look.
  15. I can help but I don't want to download, open, and run the contents of this zip. Someone else may be willing to but if you describe the problem I can help.
  16. I could in the code but I was trying to get away from doing that. The app that I'm working on shows the details for one of these models. The information that it reads the parameters from is in an XML file that it parses to JSON. It expects an image parameter and a few other key-value parameters. I want to keep the app modular to where it doesn't need to look for an image1, image2, image3, image4, etc. parameter. In a perfect world it would just read the image parameter without knowing where it came from. So if possible I want to put the conditional in the sql that loads the xml. The c-load property below is a custom tag that a script looks for and loads it's contents using the rows from the query. This takes an array of rows loaded from the community table and runs a query to find all models it's associated with in the communitymodel table. The communitymodel table does a join on the model table and the first row in modelphoto that can matches the model_id field is used as the image. The previous programmer had some setup where the models could parent other models and to get the image you would instead use model_parent to find the image in modelphoto instead of model_id. <c-load connection="main" table="community" order="community_order"> <panel name="com_modelresults_{community_name}" class="ModelResultsPanel"> <data title="Community Floorplans" start="0" limit="8"> <c-load connection="main" query="SELECT *, (SELECT modelphoto_file FROM modelphoto WHERE modelphoto_model_id=model_parent AND modelphoto_file != '' LIMIT 0, 1) as image, (SELECT modelphoto_file FROM modelphoto WHERE modelphoto_model_id=model_id AND modelphoto_file != '' LIMIT 0, 1) as image, FORMAT(model_price,0) as model_price, FORMAT(model_sqft,0) as model_sqft FROM communitymodel JOIN community ON communitymodel_community_id=community_id JOIN model ON communitymodel_model_id=model_id WHERE model_show='1' AND community_id='{community_id}' ORDER BY model_name ASC"> <option key="{model_name}" value="com_modeldetail_{community_name}_{model_name}" image="http://somesite.com/CmImageDir/{image}"> <line><attribute value="{model_maximum_beds} Bedrooms"/><attribute value="{model_bathfull_minimum} Baths"/></line> <line><attribute value="${model_price}"/><attribute value="{model_sqft} Sq. Ft."/></line> </option> </c-load> </data> </panel></c-load> So to keep this app from needing to know all this crap it would be easily reusable if it wasn't aware of where the content was coming from (abstraction of course).
  17. I've got a situation where I need a subquery to set some value in a table to a value of the first row in another table. At times this other value may be NULL so there is a possible backup value I can pull in that situation. Without doing this in the code can I do this in the query? Here is my query- SELECT *, (SELECT modelphoto_file FROM modelphoto WHERE modelphoto_model_id=model_parent AND modelphoto_file != '' LIMIT 0, 1) as image, (SELECT modelphoto_file FROM modelphoto WHERE modelphoto_model_id=model_id AND modelphoto_file != '' LIMIT 0, 1) as image, FORMAT(model_price,0) as model_price, FORMAT(model_sqft,0) as model_sqft FROM communitymodel JOIN community ON communitymodel_community_id=community_id JOIN model ON communitymodel_model_id=model_id WHERE model_show='1' AND community_id='{community_id}' ORDER BY model_name ASC The important lines are the first 2 after SELECT *,. The second will overwrite the first even if the second is an empty string. So can I wrap it in some kind of condition that says only set the value of image if a result was found. I don't even need an if-else since if the second line overwrites the first with a legitimate value it's preferred.
  18. I went ahead and used DOMDocument. Thanks for the advice from everyone.
  19. I may try to convert this to DOMDocument. Unfortunately I started using SimpleXML without realizing it doesn't have an insertBefore method and order of elements is important in my script.
  20. I've got an xml file I want to run a regular expression on. I want to load elements dynamically using an element I'm calling "c-load". These c-load elements can be nested and use the results from the previous c-load in its queries. Here is what my xml looks like- <!-- Community Results Page --><panel name="com_results" class="GridMenuPanel"> <data columns="4" rows="3" icon-width="128" icon-height="170"> <c-load connection="main" table="community" order="community_order"> <option key="{community_name}" value="com_detail_{community_name}" image="http://site/CmImageDir/{community_logo}"/> </c-load> </data></panel> <!-- Community Detail Pages --><c-load connection="main" table="community" order="community_order"> <panel name="com_detail_{community_name}" class="SlidePanel"> <data> <panel name="com_models_{community_name}"/> <panel name="com_inventory_{community_name}"/> <panel name="com_overview_{community_name}"/> </data> </panel></c-load> <!-- Community Models Pages --><c-load connection="main" table="community" order="community_order"> <c-load connection="main" query="SELECT * FROM community_model WHERE community_model_community_id='{community_id}' ORDER BY community_model_order ASC"> <panel name="com_modeldetal_{[1]community_name}_{model_name}" class="ModelResultsPanel"> <data> <option key="{model_name"} value="com_modedetail_{[1]community_name}_{model_name}"> <_attribute name="{model_name}"/> <_attribute beds="{model_bed}"/> <_attribute baths="{model_bathfull}"/> <_attribute price="{model_baseprice}"/> <_attribute sqft="{model_sqft}"/> </option> </data> </panel> </c-load></c-load> So I could write an expression like this and it will work for most- |<c-load(.*)>([\w\W\d\D]+)</c-load>|U But if c-load elements are nested like what's seen in the Community Models Pages section it will end at the first </c-load> it finds. If I wrote a custom function to pull out c-load elements I would count how many nested <c-load> elements I found with a counter and each time I run into </c-load> I would decrease the counter. If the counter is 0 then the </c-load> found would be the end of the block. <c-load> <!-- counter is 0 --> <c-load> <!-- counter += 1 --></c-load> <!-- counter != 0 - counter -= 1 --></cload> <!-- counter is 0 - return block --> So is this possible using one line of regex or do I need to come up with a custom solution?
  21. I have a situation where the window is stealing my scope so to speak. I need to create an animation script from scratch. I have an object called Frame that has a method called runAnimation. A frame can run an unlimited amount of animations asynchronously. I'm either going to need to use setTimeout or requestAnimationFrame. The problem is once I do this the scope becomes the window scope and I can't get the variables I need to run the frame. I tried to get around this a number of ways but I'm stumped. Have a look- Frame.prototype.runAnimation = function(name){var Animation = new AnimationPointer();Animation.name = name;Animation.updateLastCall();this.addAnimationPointer(Animation);if(this.isAnimating() == false){ this.isAnimating(true); // First attempt // window.PanelOSFrames[this.uniqueFrameID].updateAnimations // Second attempt requestAnimationFrame(this.updateAnimations); // Third attempt // var that = this; // that.updateAnimations}}Frame.prototype.updateAnimations = function(){var Animations = this.getAnimationPointers();console.log(Animations);} Frm.runAnimation("toggle_menu_in") Uncaught TypeError: Object [object Window] has no method 'getAnimationPointers' Any trick to change the scope? I've thought about using setTimeout so I can pass a string instead of a reference to the actual function but requestAnimationFrame is so useful.
  22. Well I found a solution but I'm not too happy with it because I'm completely confused. Doing this.test.push(1); Added 1 to both sub classes test array seen here- http://jsbin.com/ojinar/1/edit Instead I did this- this.test = [1]; Seen here- http://jsbin.com/uxacop/1/edit I can make this work for tonight but I don't know why it would work that way. Javascript needs better inheritance.
  23. Ok I think I've identified the problem but I don't know how to solve it. PSE1 = new PageSettingsEditor();PSE2 = new PageSettingsEditor();if(PSE1.prototype == PSE2.prototype){ alert("ur effed");} But this only affects arrays so far from what I can tell. http://jsbin.com/ojinar/1/edit
  24. tldr; JS Bin link at the bottom. So here is my situation. I've got a few classes that I want to have custom event listening and dispatching so I can write code like this- Obj.addEventListener("load", new Callback(this, function(){ /// do stuff after load})); Obj.prototype.load = function(){ /// do some stuff this.dispatchEvent("load");} Obj.load(); I need this because I'm doing asynchronous ajax calls and I need some processes to wait on those completing. This is the simplest and easiest to manageable in my opinion. So I'm using prototype to extend all classes off of my StandardObject class. Here is the relevant code- /** Author: Micah Williamson** Events* - error* - log* - warning*/function StandardObject(){this.eventListeners = [];}StandardObject.prototype.eventListeners = null;StandardObject.prototype.dispatchEvent = function(evtName, evtData){for(var i = 0; i < this.eventListeners.length; i++){ var Evt = this.eventListeners[i]; if(Evt.name == evtName) { var callback = Evt.callback; if(Evt.callback.toString() == "Callback") { var obj = Evt.callback.obj; var func = Evt.callback.func; obj.callback = func; obj.callback(evtData); } else if(evtData != null) obj.callback(evtData); if(Evt.once) { this.removeEventListener(Evt); } }}}StandardObject.prototype.addEventListener = function(nm, callback, once){var Evt = new Event();Evt.name = nm;Evt.callback = callback;Evt.once = once; this.eventListeners.push(Evt); return Evt;}StandardObject.prototype.removeEventListener = function(Evt){var found = false;var newEventStack = [];for(var i = 0; i < this.eventListeners.length; i++){ var E = this.eventListeners[i]; if(E == Evt) found = true; else newEventStack.push(E);}this.eventListeners = newEventStack; if(found == false) this.warn("event attempted to be removed but not found");}StandardObject.prototype.toString = function(){return "StandardObject";}// Helper StandardObjectsfunction Event(){this.name = "Undefined";this.callback = function(){};}function Callback(obj, func){this.obj = obj;this.func = func;}Callback.prototype.toString = function(){return "Callback";} (sorry for the bad indentation. dreamweaver is terrible at formatting) And the code I'm testing- /** Author: Micah Williamson** include <StandardObject.js>* PageSettingsEditor extends StandardObject** Events* - load* - urlchange* - savestart* - save*/function PageSettingsEditor(){}PageSettingsEditor.prototype = new StandardObject();PageSettingsEditor.prototype.load = function(){var that = this; // Some ajax stuff that.dispatchEvent("load", data);}// Modifiers // AccessorsPageSettingsEditor.prototype.toString = function(){return "PageSettingsEditor";} The problem is if I run this code- window.onload = function() { PSE1 = new PageSettingsEditor(); PSE2 = new PageSettingsEditor(); PSE1.addEventListener("load", new Callback(PSE1, function() { alert(this); })); PSE2.addEventListener("load", new Callback(PSE2, function() { alert(this); })); PSE1.dispatchEvent("load"); } I get 2 popups when only PSE1 dispatched load. I'm guessing this has something to do with prototype. You can see a running example with this JSBin. http://jsbin.com/okegab/1/edit So what am I missing?
×
×
  • Create New...