Categories
discuss

Calling a function in a typescript file from HTML.

I’m new to HTML and Angular 2. I’m currently trying to figure out how to call a function that’s inside a Typescript file from an HTML file.

Stripped down, the Typescript file (home.ts) function looks like this:

getConfigurations(sensorName: string) {
    console.log('Home:getConfigurations entered...');

    return 'sensor1';
}

In my HTML file (home.html), I would like to call ‘getConfigurations’ with a string parameter (right now ‘getConfigurations()’ returns ‘sensor1’ since I’m still testing things out). So, in HTML, how do I go about calling my getConfigurations method with a parameter (for simplicity – how can I call the method inside a simple div panel)? (I already have the value I want to pass in a variable called ‘selectedSensor’).

Answer

There is feature provided by angular is events binding by using you are able to call function whihc is exist in your ts file also you can use interpolation syntax of angular to call function like this : –

<button (click)='getConfigurations("parameter")'>Button Click</button>

or something like this

{{getConfigurations('parameter')}}  

for more info related to event binding in angular2 see here

working example Working Plunker

Categories
discuss

Simplifying multiple boolean checks into a single one

In one of our tests, we have the following set of expectations:

expect(headerPage.dashboard.isDisplayed()).toBe(true);
expect(headerPage.queue.isDisplayed()).toBe(true);
expect(headerPage.claimSearch.isDisplayed()).toBe(true);
expect(headerPage.claim.isDisplayed()).toBe(true);
expect(headerPage.case.isDisplayed()).toBe(true);
expect(headerPage.calendar.isDisplayed()).toBe(true);

On one hand, having multiple simple expectations provide a more precise and understandable feedback, but, on another, this looks like it violates the DRY principle and the “one expectation per test” commonly acceptable guideline.

Is there a way to convert/simplify it to a single expect?


headerPage is a Page Object, dashboard and other page object fields are navigation links.

Answer

I think you’ve misunderstood the purpose of the “one expectation per test” guideline. The point isn’t to combine a bunch of expectations into a single expectation, it’s to split up your expectations into separate tests.

To follow the spirit of that guideline, you would write your tests like this:

describe("The header page", function () {
    var headerPage;
    beforeEach(function () {
        //Common logic here
    });

    it("displays the dashboard", function () {
        expect(headerPage.dashboard.isDisplayed()).toBe(true);
    });

    it("displays the queue", function () {
        expect(headerPage.queue.isDisplayed()).toBe(true);
    });

    it("displays the claimSearch", function () {
        expect(headerPage.claimSearch.isDisplayed()).toBe(true);
    });

    //etc.
});

That’s a fair bit more verbose than what you have; but that’s why these are guidelines not rules. It’s a tradeoff between how verbose you make your tests, versus how easy they are to debug later. (“The header page displays the dashboard: FAILED”) is a very clear and specific test failure message, compared to getting the same failure message regardless of which expectation actually failed.

I definitely would not try to combine all of these lines into a single line. If you don’t want to split it into a bunch of different test cases I would just leave it how it is.

Categories
discuss

React js changing state does not update component

I have a button that when it’s clicked it should change button color to red, I am doing that by changing state to update class of component to make it .red class thus updating color and after 4s it would turn back to normal.Yet somehow it does not update and re-render the component. My code :

import React from "react";
import ReactDom from "react-dom";

const app = document.getElementById("app");
class Layout extends React.Component{
constructor(props){
    super(props);
    this.users =[
            {
                name:"user1",
                world:"88",
            },{
                name:"user12",
                world:"882",
            },{
                name:"user13",
                world:"883",
            },{
                name:"user14",
                world:"884",
            },{
                name:"user14",
                world:"884",
            },{
                name:"user15",
                world:"885",
            },{
                name:"user16",
                world:"886",
            },{
                name:"user17",
                world:"8867",
            },{
                name:"user18",
                world:"8868",
            }
    ];
    this.ulist = this.users.map((user, i) => {
        var cName = i<5 ? "active":"nonActive";
        return <div key = {i} className = {cName}><h4>{user.name}</h4><p>{user.world}</p></div>;
    });
    this.state = {
        lastUser:4,
        firstUser:0,
        errorUp:"",
        errorDown: "",
    };
}
moveUp(){
        this.state.errorUp = "red";
        setTimeout(() =>{
            this.state.errorUp = "";
        },4000);
}
render(){
    return(
        <div>
        <i className={"fa fa-caret-up arrow "+ this.state.errorUp} aria-hidden="true" onClick = {this.moveUp.bind(this)}></i>
        <i className={"fa fa-caret-down arrow "+ this.state.errorDown} aria-hidden="true"></i>
        {this.ulist}
        </div>
    );
}
}
ReactDom.render(<Layout/>,app);

Why could this be happening.And is there any other way to add class to the component so it would update.Thank you for your time;

Answer

you need to use this.setState({property:value})method instead of this.state.something = “value” to set the new state. this.state.errorUp = “red” won’t work, because setting state is an asyncronous operation, and setState method was created for that purpose.

Categories
discuss

What does this question mark mean in Flow: “?() => void”

In a GitHub project I recently saw this function declaration:

function configureStore(onComplete: ?() => void) {

What this question mark is about?

I guess, onComplete is named parameter, getting function calls. And the question mark is stating that this parameter could be optional and will default to “void”, which would mean the same like a nil/null pointer what means “no closure” assigned here.

Am I right?

Answer

Almost.

() => void is Flow’s annotation for a function that returns nothing (undefined, aka void 0).

The leading question mark in ?MyType is Flow’s way of expressing a nullable type.

So in this case configureStore accepts one argument called onComplete that must be either null or a function that returns nothing.

Flow will not add a default value for onComplete or coerce it in any way because unlike typescript, Flow does not generate any new JS code. At runtime, all Flow annotations are stripped to get vanilla JS, and that’s that.

Categories
discuss

How to pass value of an input range to a variable in JS?

How do you get the value of an input range slider into a variable? Below is the range I’m using. Suppose I drag the range to 12, then I need “12” as the variable that I want to then pass to a function.

 <input type="range" min="1" max="30" value="15" />

Edit: I don’t want a button to confirm the value or something, I want that everytime the value is changed, it gets passed to the function, so it’ll be dynamic!

PS: It may not be the best question out there, but I’ve honestly tried looking for an answer before posting the question.

Answer

If you give an id to your field:

 <input id="myRange" type="range" min="1" max="30" value="15" />

then:

 $('#myRange').val();

First step it is not really required, but it makes things easier.

You can do this in every form field element:

 $('selector').val();

And you will get its value.

UPDATE FOR YOUR QUESTION:

Use .change event to bind a function that make whatever you want to do with this value, for example:

$('#myRange').change(function(){
    var myVar = $(this).val();
    alert(myVar);
});
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..