Enumerable invoke custom method - prototypejs

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.

Related

Calling `this` in angular-meteor

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

NSubstitue mock IEnumerable Where() method

I have the following code:
45 var listMock = Substitute.For<List<EntityTestObject>>();
46 listMock.Where(Arg.Any<Func<EntityTestObject, bool>>()).Returns(Something);
But I'm getting the following error:
System.ArgumentNullException : Value cannot be null.
Parameter name: predicate
at System.Linq.Enumerable.Where[TSource](IEnumerable'1 source, Func'2 predicate)
at line 46.
However, methods that does not take func<> arguments, such as Any(), do not fail.
My questions are:
How to avoid this mistake if it possible?
If not, how to mock Enumerable Extension methods?
NSubstitute can not mock extension methods like Enumerable.Where<T>. For the case of a List I would advise not mocking it at all. Use a real list and add the items you need for your test, so that the real Where(..) extension method will filter the items and provide the items required.

Is there a way to combine page object gem and javascript calls

Im using the page object gem and selenium,
when filling in a sign up form, the form fills in correctly, but when clicking apply it errors saying the fields are required even though they are filled in.
this seems to be caused because the page object/selenium method isn't firing the javascript change method which is needed for the application to know the field has been filled in
this can be fixed by using code such as
on(SettingsPage).payment_method_account_number = number
#browser.execute_script("$('input[name=account_number]').change()")
but this is obviously not ideal and breaks the whole point of using page object in the first place by having to declare the fields name attribute again
is there a way better way to solve this problem than what i have shown?
To avoid duplicating an element definition within the page object as well as the execute_script script, you can pass the page object element to the script.
The underlying Selenium-WebDriver (and therefore the Page-Object gem) supports an arguments array within the script being executed. This arguments array basically takes a Selenium-WebDriver elements and converts them to something usable by the script. The Page-Object execute_script method handles the conversion of elements to the right type, so you simply need to:
Declare a script that uses the arguments array
Pass in a PageObject::Element
For example, let us assume your page object has used the accessor:
text_field(:payment_method_account_number, :name => 'account_number')
Therefore, the page object will have a payment_method_account_number_element method that returns the PageObject::Element for this text field.
Within the script you want to execute, you can replace how you locate the element with the arguments array and pass in the PageObject::Element to the execute_script method:
execute_script("$(arguments[0]).change();", payment_method_account_number_element)
Then you can re-write the call to on as:
on(SettingsPage) do |page|
page.payment_method_account_number = number
page.execute_script("$(arguments[0]).change();", page.payment_method_account_number_element)
end
(Or, as Dane pointed out, put this into a method in the page object.)
I have had a similar problem but the event was "onblur" instead of "onchange". I would imagine the on change would fire, but if it doesn't you could use an approach similar to mine. I ended up creating a widget that redefined the "#{name}=" method to also call the event on the spot. It's a little bit more complicated, but it centralizes all the magic to one class and keeps the code brief.

Accessing "this" from a Helper method in ASP.NET MVC 3

I have a Helper method that I need to use across multiple views. In an attempt to accomplish this, I tried to implement the approach shown by Scott Guthrie here: http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx. For the sake of reference, my helper method looks like the following:
#helper MyMethod(string parameter)
{
MyNamespace.MyClass.HelperMethod(this.Request, parameter)
}
As you can see, I need to get access to the HttpRequestBase object associated with the view. The code works fine if I define the method at the top of _Layout.cshtml. However, I need to use it in other views. So, as mentioned, I used the approach highlighted by scottgu. Unfortunately, I get a runtime error now that says:
"CS0026: Keyword 'this' is not valid in a static property, static method, or static field initializer"
How can I get over this hump?
If it's the HttpRequestBase object you need, try passing in HttpContext.Current.Request instead of this.Request.

delegate method call

I want to know simply what is the benefit of adding methods using += in delegates..
If you assigned a method to a delegate using =, you would replace any other methods that have already been assigned to that delegate. By using +=, you are adding a new method without removing any existing methods already assigned.

Resources