2014年06月

Javascript hideshow function

Hi I've been trying to get a hideshow function and came across one that works fine. The only problem for me is I would like my div to be hidden and then to be displayed onclick rather then displayed then hidden. here is a sample code:



   <!DOCTYPE html>
<html>
<head>
<script>

function hideshow(which){
if (!document.getElementById)
return
if (which.style.display=="none")
which.style.display="block"
else
which.style.display="none"
}

</script>
<style>

#adiv {
width: 320px;
height: 320px;
background: green;
}

</style>
</head>

<body>

<a href= "javascript:hideshow(document.getElementById('adiv'));";>clickme!</a>

<div id="adiv"> </div>


Hope this has enough info :)



Answers

You can initially hide the div in two popular ways:



(1) Hide using CSS



Add display:none to the div to hide the div whether JS is enabled or not. You can use something like this:



<div id="adiv" style="display:none"> </div>


(2) Hide using JS



Hiding via JS is useful if you don't want the div hidden for users that don't have JS enabled. There are various ways to hide the div using automatically executing JS. Below is a simple way to do this.



<body onload="hideshow(document.getElementById('adiv'));">

<a href= "javascript:hideshow(document.getElementById('adiv'));";>clickme!</a>
<div id="adiv"> </div>


Answers

I would like my div to be hidden and then to be displayed onclick rather then displayed then hidden




Trying to understand what you mean above, I can't guess anything else that you're talking about the original state of the <div>.

At the same time, I can't imagine you don't know how to achieve it!



Anyway, if that is really the question, it's pretty simple: add display: none; to your #adivrule.



BTW, you should prefer standard-compliant style for coding, to improve readability, like this:



   function hideShow(which){
if (!document.getElementById) {
return;
}
if (which.style.display == "none") {
which.style.display = "block";
} else {
which.style.display = "none";
}
}


And finally, you might also enhance it, so:



   function hideShow(which){
if (document.getElementById) {
which.style.display = which.style.display == "none" ?
"block" : "none";
}
}




iCloud and non-UIDocument files

My app reads and writes audio files, using AVFoundation’s methods for file operation.



I would like to incorporate iCloud sync, with the intended outcome of having my app’s entire root Documents directory synced to the cloud.



I’m not really sure how to implement this. The Apple documentation says that iCloud syncing is free with files managed by UIDocument and that NSFileCoordinator needs to be implemented for all other file operations.



Do I need to somehow ‘wrap’ AVPlayer and AVRecorder’s file reading and creation calls in that file coordinator?





How do multiple versions of a REST API share the same data model?

There is a ton of documentation on academic theory and best practices on how to manage versioning for RESTful Web Services, however I have not seen much discussion on how multiple REST APIs interact with data.



I'd like to see various architectural strategies or documentation on how to handle hosting multiple versions of your app that rely on the same data pool.



For instance, suppose you make a database level destructive change to a database table that causes you to have to increment your major API version to v2.
Now at any given time, users could be interacting with the v1 web service and the v2 web service at the same time and creating data that is visible and editable by both services. How should this be handled?



Answers

Most of changes introduced to API affect the content of the response, till changes introduced are incremental this is not a very big problem (note: you should never expose the exact DB model directly to the clients).



When you make a destructive/significant change to DB model and new API version of API is introduced, there are two options:




Turn the previous version off, filter out all queries to reply with 301 and new location.
If 1. is impossible to need to maintain both previous and current version of the API. Since this might time and money consuming it should be done only for some time and finally previous version should be turned off.


What with DB model? When two versions of API are active at the same time I'd try to keep the DB model as consistent as possible - having in mind that running two versions at the same time is just temporary. But as I wrote earlier, DB model should never be exposed directly to the clients - this may help you to avoid a lot of problems.



Answers

I have given this a little thought...



One solution may be this:



Just because the v1 API should not change, it doesn't mean the underlying implementation cannot change. You can modify the v1 implementation code to set a default value, omit the saving of a field, return an unchecked exception, or do some kind of computational logic that helps the v1 API to be compatible with the shared datasource. Then, implement a better, cleaner, more idealistic implementation in v2.





Get Product Options for each Product in the Catalog in Magento

This is using Magento 1.9.



I am trying to build a script that outputs all the product options for each product in the catalog into a CSV file.



I have tried using the code on this page: http://www.atwix.com/magento/configurable-product-attributes/



For this code:



$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');  
foreach ($attribute->getSource()->getAllOptions(true) as $option) {
echo $option['value'] . ' ' . $option['label'] . "\n";
}


I get no output, what-so-ever.



For this code:



$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute) {
foreach ($productAttribute['values'] as $attribute) {
$attributeOptions[$productAttribute['label']][$attribute['value_index']] = $attribute['store_label'];
}
}


I get this error:



Fatal error: Call to undefined method Mage_Catalog_Model_Product_Type_Simple::getConfigurableAttributesAsArray() in /home/tws2/public_html/getopts.php on line 71



Ive been unable to find anything else on the internet for getting at the product options of a product with PHP code.



Can someone please explain to me how to use PHP to get at all the product options data for each product in the catalog, so I can output all this data into a CSV file.



The purpose of this is to transfer the product options data over to an identical Magento server that has the same catalog, just missing the product options data. Once I have all the CSV file of product options I was going to make another script to write them into the identical Magento server. This is how Ive been advised to do this task in-house, if you have a better suggestion please let me know.



Answers

To get the configurable options of whole catalog, try this



$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');

foreach ($collection as $product) {
foreach ($product->getProductOptionsCollection() as $o) {
$values = $o->getValues();

foreach ($values as $v) {
$v['product_id'] = $product->getEntityId();
echo "<pre>";print_r($v->getData());
}
}
}




↑このページのトップヘ