Categories
discuss

How to set the jslint ES6 directive in Brackets?

I get this error: “expected an identifier and instead saw ‘const'”, I’m using brackets text editor. I found this answer: “You need to specify the es6 directive. See JSLint Help”. But I can’t figure it out how to specity the es6 directive in brackets. Here’s my code:

const singleQuotes = '<p>Single quotes</p>';
const doubleQuotes = "<p>Double quotes</p>";
const stringLiterals =  `<p>String literlas</p>`;

const result = singleQuotes + doubleQuotes + stringLiterals;
document.querySelector('.basic').innerHTML = result;

I appreciate any help. It works fine if I use var.

Answer

One File

If I remember right, you put the following at the very top of your file:

/*jslint es6:true*/

Project Wide

If you want all files in your project to abide ES6, you can add a .brackets.json file to the root of your project.

Brackets Wide

If you want all files to abide ES6, you can modify your User-global preferences by clicking the following menu items: Debug -> Open Preferences and then adding the jslint.options property to your preferences.

See How-to-Use-Brackets: Preferences for more information.


However, I just tried to do this with my Brackets installation with all extensions disabled and Brackets doesn’t understand the directive. I think Brackets has a very old version of jslint installed by default. You may want to use some extensions to try to supplement the old version.

What I ended up doing was installing the brackets-jshint and changing my brackets preferences file to use jshint by default with the following option:

{
    "language": {
        "javascript": {
            "linting.prefer": [
                "JSHint"
            ],
            "linting.usePreferredOnly": true
        }
    }
}
Categories
discuss

Removing/Adding constraint programmatically in ConstraintLayout

I want to remove and add constraint programmatically based on some condition. Here are the screenshots:

The button "create ad" has a top constraint

and I want to remove it like this but in code:

here is button with removed constraint on top

so the same effect in want to achieve programmatically

and here is the code that I tried:

    if (advertisements.size() > 0) { //my own condition
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
        layoutParams.topToBottom = R.id.imvEmpty; //the imageview that is in center of the view
        btnCreateAd.setLayoutParams(layoutParams);
        recyclerView.setVisibility(View.VISIBLE);
        txvMyAdEmptyText.setVisibility(View.GONE);
        imvEmpty.setVisibility(View.GONE);
        adapter.setList(advertisements);
        adapter.notifyDataSetChanged();
    } else {
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();
        layoutParams.topToBottom = -1; //here i am trying to remove top constraint but it doesn't work
        btnCreateAd.setLayoutParams(layoutParams);

        recyclerView.setVisibility(View.GONE);
        txvMyAdEmptyText.setVisibility(View.VISIBLE);
        imvEmpty.setVisibility(View.VISIBLE);
        adapter.setList(new ArrayList<Advertisement>());
    }
    mConstraintView.invalidate(); //this is my constraint view

EDIT

I have tried using ConstraintSet also, but the result was even different somehow my RecyclerView (which is set to boundaries of parent view) was disappearing

 ConstraintSet set = new ConstraintSet();
    set.clone(parentView);

    if (advertisements.size() > 0) {

        recyclerView.setVisibility(View.VISIBLE);
        txvMyAdEmptyText.setVisibility(View.GONE);
        imvEmpty.setVisibility(View.GONE);
        adapter.setList(advertisements);
        adapter.notifyDataSetChanged();

    } else {

        set.connect(btnCreateAd.getId(), ConstraintSet.TOP, imvEmpty.getId(), ConstraintSet.BOTTOM, 0);

        recyclerView.setVisibility(View.GONE);
        txvMyAdEmptyText.setVisibility(View.VISIBLE);
        imvEmpty.setVisibility(View.VISIBLE);
        adapter.setList(new ArrayList<Advertisement>());
    }
    set.connect(btnCreateAd.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);
    set.connect(btnCreateAd.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);
    set.connect(btnCreateAd.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);

    set.applyTo(parentView);

Answer

I have not worked through your code, but the following illustrates how to break and make the constraint using ConstraintSet.

ConstraintSet set = new ConstraintSet();
ConstraintLayout layout;

layout = (ConstraintLayout) findViewById(R.id.layout);
set.clone(layout);
// The following breaks the connection.
set.clear(R.id.bottomText, ConstraintSet.TOP);
// Comment out line above and uncomment line below to make the connection.
// set.connect(R.id.bottomText, ConstraintSet.TOP, R.id.imageView, ConstraintSet.BOTTOM, 0);
set.applyTo(layout);
Categories
discuss

What advantage is there to using Spring @Async vs. CompleteableFuture directly?

What’s the advantage of using Spring Async vs. Just returning the CompletableFuture on your own?

Answer

Your application is managed by the container. Since it’s discouraged to spawn Threads on you own, you can let the container inject a managed Executor.

@Service
class MyService {
  @Autowired
  private Executor executor;

  public CompletableFuture<?> compute() {
    return CompletableFuture.supplyAsync(() -> /* compute value */, executor);
  }
}
Categories
discuss

@PostConstruct annotation and spring lifecycle

I’m new to Spring, I would like to know:

I have a java class annotated with @Component (spring) and inside I have a method annotated with @PostConstruct. The class is then referenced by @Autowired annotated field in another class. Can I assume that the class is only injected after @PostConstruct is called?

@Component
class AuthenticationMetrics {

    private static final MetricRegistry metrics = new MetricRegistry();

    final Counter requestsTotal;

    final Meter guestLogins;

    final Meter kfUserLogins;

    final Timer guestLoginResponseTime;

    final Timer kfLoginResponseTime;

    @PostConstruct
    public void populateMetricsRegistry() {
        metrics.counter("authentication.requests.totals");
    }
}

Answer

If you are asking is injection of given class happening after @PostConstruct in that bean is called, then the answer is yes – @PostConstruct is executed before bean is considered as “injectable”

If you are asking if @PostConstruct on given bean is executed after all injections has been done (on the same bean) – then yes – @PostConstruct is executed after injections are commited to given bean. This is the reason it exists. Normally you could put @PostConstruct actions into the constructor. However, when new object is created (constructor is called) injections are not performed yet – so any initialization that depends on injected objects would fail due to NPE. That is why you need @PostConstruct

Categories
discuss

Getting the ref from a dynamic component when using Redux, React and react-router-dom 4.x

I have the following class

class MatchBox extends React.Component {
    constructor(props) {
        super(props);

        this.countdownHandler = null;
        this.showBlocker = true;

        this.start = this.start.bind(this);
    }

    start() {
        ...
    }

    render() {
        ...

        return (
            <div style={ styles.mainContainer } className="fluid-container">
                ...
            </div>
        );
    }
};

function mapStateToProps(state) {
    ...
}

function matchDispatchToProps(dispatch) {
    ...
}

export default withRouter(connect(mapStateToProps, matchDispatchToProps, null, { withRef: true })(MatchBox));

which is used in this class

class GameBox extends React.Component {
    constructor(props) {
        super(props);

        ...
    }

    render() {
        var mainElement = null;
        switch(this.props.mainElement.element) {
            case 'SEARCHING': mainElement = <SearchingBox gameType={ this.props.gameType }/>; break;
            case 'MATCH': mainElement = <MatchBox ref='matchBox'/>; break;

            default: mainElement = <SearchingBox/>;
        }

        return (
            <div style={ styles.mainContainer } className="fluid-container">
                { mainElement }
            </div>
        );
    }
};

function mapStateToProps(state) {
    ...
}

function matchDispatchToProps(dispatch) {
    ...
}

export default withRouter(connect(mapStateToProps, matchDispatchToProps, null, { withRef: true })(GameBox));

And I can’t get the ref of the object MatchBox. I tried with this.refs.matchBox and is null, also tried getting directly from ref(ref={(r) => { // r is null } }) and I don’t know what to try anymore. I’m using react-router-dom 4 and I don’t know if function withRouter affect the outcome component.

Answer

It’s not pretty, but I think this is the solution. withRouter exposes the child ref via a wrappedComponentRef callback, which gets us to the connect hoc. That exposes its child ref via getWrappedInstance if you pass the withRef attribute as you did. So you just have to combine both of those.

class GameBox extends React.Component {    
    matchboxRefCallback = (connectHOC) => {
        this.matchboxRef = connectHOC ? connectHOC.getWrappedInstance() : null;
    }
    render() {
        return <MatchBox wrappedComponentRef={this.matchboxRefCallback}/>;
    }
}
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..