So I'm new to OO Programming. Coming from a WebDesign Background and having done mostly design work or some more or less intermediate scripting wherever it was needed in my projects. But being eager to learn and still searching for the "right" way ;-) to do things like you guys do. Not what my Script Kiddie Mind tells me :D. So sorry if that is really a super beginner question, but I would rather grasp the "right" concept from the start.
I'm programmatically populating a NSMenu with NSMenuItems based on contents of a folder located in my Documents Directory. I'm iterating through the subfolders of this fellow, getting the URL of each and every one, extracting the folder name by using
let folder = el.URLByDeletingPathExtension?.lastPathComponent
and creating NSMenuItems based on those names
let menuItem = statusMenu.insertItemWithTitle(folder!, action: #selector(StatusMenuController.doFancyStuff), keyEquivalent: "", atIndex:0)"
So far so good. Now how would I store the associated URL along with the MenuItem for later use like manipulating it when the action gets triggered?
By clicking on the item I want an action to be performed, which needs the full folder URL of course. My first idea was to extend NSMenuItem with a new variable called "path" or so. But since there are no stored values allowed in extensions this is a dead end.
How do you real programmers handle such cases? Using a dictionary and storing the Items along with their respective URLs? Could work, or not? But would that be the correct way? Or getting the full url by taking the MenuItems Title and "reattaching" it to the basefolder URL? Which could also work. Or do you hand the URL to the method/action that will get called as an attribute? I would really love to stay away from messy code :) so your pro insights are very much appreciated.
Thank you for your Time!
andy
NSMenuItem has a property representedObject which can be used for that purpose.
From the documentation:
By setting a represented object for a menu item, you make an
association between the menu item and that object. The represented
object functions as a more specific form of tag that allows you to
associate any object, not just an arbitrary integer, with the items in
a menu.
If you want to store more information than just an URL you could also assign a collection object like Array or Dictionary or even a custom class to representedObject.
Related
Some coding books mention that we should not prefix the type in the name, since current IDEs are able to make these visible and clear. I completely agree for primitive types but I'm inclined to still prefix my GUI components variable names.
What are the best practices when working with on GUI code regarding GUI component variable naming?
E.g. should I name my
Button btnSubmit
TextField tfName
Checkbox cbActive
or
Button submit
TextField name
Checkbox active
I find that generally, when naming GUI-components, prefixing the variable name with the type of GUI-component simplifies the process of writing code in that when I write code that references them, I don't have to remember the exact name of each component. E.g. often I know that I'm gonna get the text from a specific TextBox, but I don't remember the exact name of it. I start typing TextBox and up in IntelliSense a range of TextBoxes appear, from which I easily can identify the exact one I'm in need of.
Regarding if it's any best practices; It depends on
If your company has any standards, I'd assume they'd be the best practices in this case.
If it's a private project, go with what you feel is the best.
My 0.02$
I'm trying to implement a list of items on a web page in which each item on the list is reorder-able via drag and drop. The current placement of an item within the list should set an 'interest' property on the item's model.
I've never built anything like this before and not really sure where to start or what approach to take. Any guidance would be very helpful!
You can use acts_as_list gem to simplify reorder process at backend, and jQuery.sortable at frontend
I found this walkthrough which was very helpful:
http://webtempest.com/sortable-list-in-ruby-on-rails-3-almost-unobtrusive-jquery/
I am writing a cms (on .net) and have structured the whole page to work client side.
There is a treeview that lets you add/remove/move items and define their names in the languages defined. For each language I save the names of the category defined, but when there is HTML content associated with it, i fall into the JavaScript serializer problem that finds the content too long to be serialized.
What would be the best approach to make sth like this work. Shall I change everything to work with postbacks, or try to manually call _doPostBack for the editor content (which I don't want). Thank you in advance.
I guess would be great to make auto-save with time interval which will submit only diffs between current state and previous save. It will do the key if the user will edit it manually, not for copy/paste, of course. It is if we talk about really big data that we need to save.
Otherwise need to find some ways to compress the data before submitting: json+base64, etc.
Having no luck here trying to find a solution to this.
I've set up a looping selector based on this guide. (I also used the ListLoopingDataSource class in Part 2 in order to have strings in my looping list).
What I want to be able to do is then change another element (an image box/placeholder) on my layout, based on what is currently selected in the looping selector.
No real idea how to do this, the onSelectionChanged event is kind of abstracted and not really useful? I'm not sure how to programmatically change images either, it doesn't look like I can access a resource from the code base, only from the xaml.
Any help/suggestions would be greatly appreciated.
I found that I could do this by using binding.
I'd like to add a new tab to the 'catalog->manage products->product information' page in the admin. Underneath the 'images' tab, I'd like to add a new tab for video, with a simple text input for adding a video url, which I can then grab for the frontend.
Anyone any ideas where the files are that I need to edit? I've been looking for the last couple hours with no joy. I found the list of current options in Mage_Catalog_Model_Resource_Eav_Mysql4_Setup but can't figure out how to add to them...
Seems it gets some from Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs. If I copy one of the tab sections here and change the title to Video, I can get it to display in the tabs on the left. But how do I then add the options for it?
What you are trying to do, from the sounds of it, is to create a custom field and add it to your catalog data. The good news is that you don't need to muck around in the PHP for that.
Head to Catalog -> Attributes -> Manage Attributes and create a new attribute for yourself called "Video URL" (or something to that effect). This will probably be a text field, and you may want to hide it from comparison on the frontend of the site (select "No" for all those boxes at the bottom of the form).
Once you've created an attribute, you will need to add it to an attribute set. If all your products are of one "type", and if you didn't create any other attribute sets, this should be only one step. Head to Catalog -> Attributes -> Manage Attribute Sets and create a "New Group" called "Video" and drag your new video url attribute into it. Save the attribute set and you should now have your new tab.
The only complication from what I read in your post could be positioning it underneath the images tab. Magento adds several tabs statically (the long way, in the PHP) and doesn't generally obey ordering of the tab groups. Consider the time tradeoff.
Hope that helps. Thanks,
Joe
Per your other comments, for the URLs a simple attribute would work but files would tend to be more difficult. To add a custom tab, as you said, you can edit (or in the case of a plugin, override) Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs::_prepareLayout and add your tab. You'll need to add a block or template to display your tab contents.
After that, you'll need to store the data somewhere. Looking in Adminhtml/controllers/ProductController, you should be able to hook into the catalog_product_prepare_save event to get data from $event->getRequest() and push it onto the product or save another entity as necessary.
I'm not sure how well file saving semantics would work on a product, so you may want to do your own data storage in the extension and then provide something like a getVideoForProduct method to retrieve it.
Then the only thing left to do is modify the catalog/product/view.phtml template file to include your thumbs and create a controller in your extension to provide a modal w/ the video (or do it in JS).
I know that's a bit of a general answer, but it's the rough path you would need to take for tabs. Let me know if any specific part of that is unclear.
Thanks,
Joe