I noticed that console.log(this.object) will return undefined if I call a function from the Controller, but if I used a button in the UI to invoke it, it will return the object.
When this is inspected, the this.object invoke property getter which probably is what happens when I use the button to call my function.
Why is this happening and how can I access the this.object from within the Controller?
Note that this might not be the best solution, but getting the object by calling for the collection in this.autorun solves the issue
Related
I am a new to laravel and trying to understand where the view method comes from and what mechanism allows it to be shown in the web.php folder in laravel.
For example :
Route::get('/', function () { return view('welcome'); })
I guess the view function is defined in some class. Bu which class is it and where is that class made reference to in order to access its method?
Thanks a lot if you can help me understanding this!
In most IDEs you can hold CTRL and left-click the function to view it's definition. view() is not defined in a class. It comes from a file called helpers.php.
This file is included at the beginning, so its functions can be used afterwards.
PHP is not only object oriented. Procedural and object oriented programming can be mixed together.
What I do usually in these cases is to search in the whole project (and remember to include vendor directory in your search) for: "function YOUR_FUNCTION_NAME" because somewhere in PHP there must be that function declared, whether is in a class or in a simple .php file.
view() method is a helper method inside src/Illuminate/Foundation/helpers.php. All the methods that declare here will be available everywhere inside Laravel application. You can check view() method here
In PrototypeJS, the invoke method works nicely with an enumeration like,
$$('.mytags').invoke('setStyle',{fontWeight:bold})
I'm having trouble making this work against my own custom methods. What is the trick?
Let say I have:
function myMethod(element) {
element.insert('bingo');
}
If I try,
$$('.mytags').invoke('myMethod');
The code fails: TypeError: Cannot read property 'apply' of undefined
Any ideas?
myMethod() needs to be a method on the prototype of the items in the array.
for instance setStyle() is the method $('id').setStyle()
to accomplish what you are trying to do you need to define your method first and then add it to the prototype of the items you are trying to iterate over.
If the items are Elements then you should be able to use Element.addMethods()
http://api.prototypejs.org/dom/Element/addMethods/
to add the methods to the tags you want or add to all elements.
I was confused with the order that which function Activate function or bind function gets called when component become active. In my opinion activate function function will be called first as Bind function is for binding the service. But as we know that all the target service are first get into the component context then component gets activated.
Please clear my doubt.
The activate method will be called after all the static references have been bound, i.e. after the bind methods have been called. So during the activate, you can be sure that the value of the static references will not change.
However for dynamic references, all bets are off. In fact the value of a dynamic reference could change multiple times in different threads, during the execution of the activate method.
UPDATE: You didn't ask about deactivation, but you might find this information useful all the same. The deactivate method will be called before any static reference is unbound. So for example: if you were bound to a service with a static reference and the service you were bound to goes away, then SCR will first call your deactivate, then your unbind method(s), and finally it will free the component instance for garbage collection.
I have a function that returns a string in Controller. So how can i invoke that function when a button is clicked in view?
Perhaps this function would be better suited in your model, especially if it perhaps refers to model properties.
Therefore instead of passing a string value to it, this can be accessed within the scope of the function:
#(model.Function())
If you function is generic, then instead of storing this in the controller, or model, I would recommend storing it in a different class altogether in a seperate namespace:
#(Logic.StringFunctions.Function(model.myString))
As others have pointed out, there may be better options, depending on what it is exactly that you're trying to do. But if your desire is simply to call a method with the click of a button, the simplest way I know of to do that is:
<button type="button" onclick="window.location.href='<%=Url.Action("MethodName", "ControllerName") %>'">
Hope that helps!
I'm working on an ASP.NET MVC3 app, and I'd like to make a call from the view to the controller and store that information as a Dictionary.
Controller code:
public Dictionary<string,int> foo()
{
Dictionary<string,int> bar = new Dictionary<string,int>();
bar.Add("test",100);
return bar;
}
View code:
#{ Dictionary<string,int> foobar = Html.Action("foo"); }
...
<div>#foobar["test"]</div>
I can get the view to work if I use var foobar = Html.Action("foo");, but then it just says that foobar is of type System.Web.Mvc.MvcHtmlString, so I can't do much with it.
Is there any built-in functionality that I'm missing, or should I just use something like a JSON result for this?
EDIT: Additional Info
It should be noted also, that in the VS2010 debugger, it recognizes foobar properly as {System.Collections.Generic.Dictionary'2[System.String,System.Int32]}, so the issue is that foobar isn't resolving correctly. The error thrown is:
Compiler Error Message: CS0021: Cannot apply indexing with []
to an expression of type 'System.Web.Mvc.MvcHtmlString'
EDIT 2
These are the errors that came with casting the result to a dictionary:
Url.Action("foo").ToDictionary<string,int>(); returned CS1501: No overload for method 'ToDictionary' takes 0 arguments for obvious reasons, but the VS2010 debugger recognizes that the result is a Dictionary:
But when I add parameters (Url.Action("foo").ToDictionary<string,int>(x=>x);), it stops recognizing that it is a dictionary, although I'm not sure those are the proper params (it's based off what I found here.
You should try as much as possible to keep views dumb. The controller has to push the data to the view. For some reason if you want to perform call a function that performs complex calculations then a better idea would be create a separate static class and move the function from controller to that class. The Html.Action is for entirely a different purpose. Your controllers should always return action results not dictionaries or other types.
A call to Html.Action("foo") is supposed to return a string. What this is doing is calling an action method that renders the child view as a string. See http://msdn.microsoft.com/en-us/library/ee721266
You could call a static utility method , see Is this an Extension or Helper Method in MVC3?