Backbone - binding to a collection's reset event loses reference to `this` - scope

I don't think this is an uncommon problem, but I haven't been able to make any of the solutions I've found work. Here's my [simplified] view:
class MyView extends Backbone.View
el: '#mydiv'
initialize: ->
#collection.bind 'reset', #render, #
render: ->
$(#el).html('my content')
When reset is triggered, render's this has been clobbered, and #el is undefined. My understanding was that the 3rd parameter on bind was supposed to take care of this, but that doesn't appear to be happening. I am using Backbone 0.5.3.
I also tried using the "fat arrow" on render, but that didn't work either:
render: =>
$(#el).html('my content')
Update
As Trevor Burnham pointed out below, it wasn't a scoping issue, it was that my el property wasn't available at page load (it get's created later). I'm still looking for a better way to deal with that (using the id property on the view).

I don't think the problem is that render is called in the wrong context, but rather that the view's el property is never a DOM element. Does something with the ID mydiv exist at the time that you call new MyView? If not, that's the problem.
Internally, when el is a string, Backbone makes the call
this.el = $(this.el).get(0);
from the view's constructor. If nothing matching that selector string exists, #el will be undefined, which is what you're seeing.

Use the double arrow => instead of single-arrow -> when defining your render method, and CoffeeScript makes sure that the this pointer points to the class instance. Also, you may want to try to set the id member to the id reference instead of the el member, as the documentation for backbone (http://documentcloud.github.com/backbone/#View-el) indicates that el gets created (by backbone itself) based on whatever id, tagName and className is set to.

Related

How to know when a property has been updated from it's connected attribute?

Given:
#property({type: Boolean, attribute: 'some-attr'}) someAttr = false;
I was expecting to see updated being fired once 'some-attr' value gets updated in the DOM.
However, updated doesn't get fired at all.
Is my expectation wrong, or should I set things up differently?
Looking at Elm's discussion of properties vs attributes, the documentation of the Html.Attributes module's attribute function, and the Elm documentation on custom elements, I am pretty sure, that this is caused by simply binding an elm expression to attribute some-attr of the LitElement based custom element. I.e. the DOM attribute will always be present and hence the corresponding property always be true.
The default converter for Boolean (activated by providing type:Boolean to the decorator) mimicks the behaviour of HTML attributes used as flags (e.g. disabled on an <input> element): If the attribute is present (no matter the value), the flag is set (true). The implementation is really straight forward, if you want to look at it in the sources: https://github.com/Polymer/lit-element/blob/master/src/lib/updating-element.ts#L163
I see these options for your problem:
Implement some extra logic in Elm to add / remove the presence of the attribute.
Create your own attribute converter for the LitElement based custom element.
Use another default converter (e.g. for String, the "default" default converter) and implement the custom logic inside the LitElement (e.g. using a derived value).
Of these 3 options, I would generally recommend the first one, as your custom element then still behaves naturally, i.e. if some-attr should be a flag (boolean attribute), then following which HTML semantics, it should be defined by its presence, not its value. This allows you to re-use it in other projects without surprising other developers.
That being said, there may of course be project-specific requirements, that are more important. E.g. if you only use this custom element in this one project with Elm, your road to success may be faster going for options 2 or 3.

Cannot get itemDescription on f:selectItems to have an effect

The doc says:
"itemDescription: evaluates to a String that will serve as the description to be shown for the item."
http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/selectItems.html
http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/selectItem.html
I'm not getting any title attribute added to the resulting option element in the DOM or anything, not even when setting a literal String as its value (neither on f:selectItem nor f:selectItems, each tried seperately, the latter with a c:forEach over the list items which showed up correctly with their labels).
Some forum posts seemed to suggest people use it for tooltips.
The doc isn't being entirely clear, is the itemDescription attribute on the component even meant to be rendered as a tooltip/title? And if not, what's it good for?
I think BalusC already answered it in https://stackoverflow.com/a/25512124/3280015, which I initially overlooked.
"While creating the custom renderer, you could make use of the unused(!) description property of the UISelectItem class."
So it is currently simply unused and left for potential use by developers. Maybe that's what the Doc means by:
"for use in development tools."

Passing an object from a tabbarController to its subviews

I am trying to pass a simple core data objects info from a tabBarController to its subviews so that they each reference a different attribute of that object. As a newbie, I'm not sure even where to start. It doesn't seem to be as simple as passing the data from one tableView to another...
Thank you for any help.
If you are sharing the same object between (most of the) the view controllers of your tab bar controller, maybe the best architecture for this would be to have one central data object.
A typical pattern is a singleton, some kind of data manager that provides the object, but maybe that is overkill. Another is to keep references to all view controllers and update them one by one when something changes - also not very elegant.
What you really want is something like a global variable. You could (ab)use your app delegate (just give it a property that points to the object) or if you prefer even your tab bar controller (make a subclass, give it a property). In the latter case, every view controller could then get the object like this:
NSManagedObject *object = [(MyCustomTabBarController*)self.tabBarController object];
For example, you can check for changes and refresh your views in viewWillAppear.
A UITabBarController should be handling other view controllers, not handling data objects. How does the tab bar controller get the object reference in the first place? And what is the object you're sharing?
Let each of your subordinate VC's keep a pointer to the object, and then they can each follow the appropriate keypath to get to the entities they're designed to handle.
Tim Roadley's book Learning Core Data for iOS, in chapters 5 and 6, shows how to pass an object from one view controller (a table view) to a detail view. It doesn't sound like that's what you're asking, but just in case...
In response to comment:
I'm looking at a tableview, tap a cell, and then a tab bar controller slides in? That's not the usual visual metaphor for a tab bar; it's meant for changing modes for the entire program. See the Music app for a typical example: songs, playlists, artists.
But if you really need to do it that way, try this (I'm assuming you're using storyboards):
In prepareForSegue: in your tableview controller, tell the destination (tab bar controller) what object it's working with.
In the tab bar controller's -viewWillAppear, tell each of its tabs about the attribute: self.frobisherViewController.frobisher = self.myWidget.frobisher.
You could instead tell each of the component tabs about the top level object: self.frobisherViewController.widget = self.myWidget. But I like the first approach better because there is less linkage. The frobisherViewController now would need to know about both widgets and frobishers.
This ended up being very simple. I was trying to call the object in the child views initWithNibName which doesn't work. I ended up creating a setObject function and calling the properties I wanted in viewWillAppear.
Hope this helps someone.

Changes to DOM objects are lost after partial update

On a page I have some fields that I want to be "readonly" (in my meaning they can't be accessed but they will store values, read earlier question in this matter if issues...).
I use a client JS setting these attributes on page load:
$(".readonly").attr('readonly', true);
If I have a partial update on any of these fields the attribute is lost and the field is accessible.
What is the best practice to overcome this and make it work?
Every partial refresh has a oncomplete method bound to it. What you could do is add code to the oncomplete method so the item is being set readonly again. Another, better, approach would be not to change the attribute clientside but to have hidden fields which are used to store the data.
When you have an event bound to for instance an Link control you can change the oncomplete code by clicking in your source pane on the event tag. When you browse the events section in the properties pane you will see the onComplete, onError, onStart properties. You can add your own clientside script in these properties.
Before trying to overcome the "problem" You shoud try to understand what exactly partial refresh do and where the state of application is kept.
Unfortunately partial refresh is replacing current html content (or rather part of it) with a newly created one and only form fields that has backing controls will keep state.
I suggest You should try setting readonly property on controls which You would like to make readonly (if there is some logic here You can always use ssjs).
Optionally You can try to preserve the state on the client side by using csjs global variables but this is rather hard to manage.
And one more thing - try to use the technology to solve the problem(xpages) and try not to hack Your way through with use of stuff that You accidentally know (jquery).
I would agree with jjtbsomhorst on using onComplete event. But another alternative could be setting the readonly property via code like this:
var textField:com.ibm.xsp.component.xp.XspInputText = getComponent("inputText1");
var readOnlyAttr:com.ibm.xsp.complex.Attr = new com.ibm.xsp.complex.Attr("readonly", "readonly");
var list:java.util.ArrayList = new java.util.ArrayList();
list.add(readOnlyAttr);
textField.setAttrs(list);
You could call this on the afterPageLoad event of the XPage. But I really don't know whether this would be the best way to about!

Reflection does not reflect method in interface in C#

I cannot figure out what the deal is with this code. "command" = Park, and the Park method exists in the interface. "method", however, just equals null, so it throws an exception when I hit invoke. The debugger kicks in, and so I was able to make sure that everything looks OK. Why does method not get set?
The scope object is defined. It is a com object. I expanded the dynamic view of the scope object and all it shows is properties or something (blue 3d boxes, not hand and paper), no methods. Is that the problem? But why, since I am referring to ITelescope and don't do anything with scope until I try to invoke the method, which was not set.
I got the Activator code from this question.
ITelescope scope = (ITelescope)Activator.CreateInstance(Type.GetTypeFromProgID(scopeProgID));
var method = typeof(ITelescope).GetMethod(command);
method.Invoke( scope, new object[0]{ } );
I remembered that I had asked another question about interfaces and reflection. The problem has something to do with interop types. To fix it you right click on the offending reference in the reference list of the project, click properties, and set "embed interop types" to false.

Resources