Categories
discuss

Can I break a chain early with bluebird Promises?

I don’t necessarily want to error, but I have:

getFromDb().then (tradeData) ->
  if not tradeData
    # DO NOT CONTINUE THE CHAIN
  else
    getLatestPrice tradeData
.then (latestPrice) ->
  ...
.then ->
  ...
.then ->
  ...
.catch (err) ->
  next err

Any way for me to abort the chain if there is no tradeData?

Answer

getFromDb().then (tradeData) ->
  if tradeData
    getLatestPrice tradeData ->
      .then (latestPrice) ->
        ...
      .then ->
        ...
      .then ->
        ...
      .catch (err) ->
        next err
  else
    getSomethingElse ->
       send("no data")

In 3.0, you will be able to do this:

p = getFromDb().then (tradeData) ->
  if not tradeData
    send("no data");
    p.break()
  else
    getLatestPrice tradeData
.then (latestPrice) ->
  ...
.then ->
  ...
.then ->
  ...
.catch (err) ->
  next err
Categories
discuss

How to select unique values in d3.js from data

I am using the following code to populate my Combobox using d3.js

d3.csv("Results_New.txt", function(data) {
dataset=data;
d3.select("#road").selectAll("option")
.data(data).enter().append("option").text(function(d){return d.roadname;}).attr("value",function(d){return d.roadname;});
});

There are different rows with the same name. The combobox is populating with these duplicate names. For example in multiple rows the name of the road is I-80.But in the combobox I want to see I-80 only once. How can I do that?

Answer

Filter your data retaining unique keys only by d3.map

d3.select("#road").selectAll("option")
    .data(d3.map(data, function(d){return d.roadname;}).keys())
    .enter()
    .append("option")
    .text(function(d){return d;})
    .attr("value",function(d){return d;});
Categories
discuss

ShareActionProvider appearance

I have android.support.v7.widget.ShareActionProvider menu in my ActionBar.

When I click “Share”, the app list appears as a popup menu.

When I click “Share” in Google Play Newsstand, the app list appears as a bottom sheet that can be pulled up.

Can we configure ShareActionProvider from appcompat-v7 to display bottom sheet instead of popup menu? Are there any alternative ShareActionProvider with bottom sheet around?

I found a lib https://github.com/soarcn/BottomSheet. It gives an idea how to re-implement the ShareActionProvider with bottom sheet. Unfortunately it looks like the lib is missing ‘swipe up to pull up’ at this moment. And more over, I still have to resolve intents, handle screen rotations and support all the Android versions… Shouldn’t this be included in appcompat-v7?

Answer

The solution is to use Intent.createChooser instead of ShareActionProvider. It gives exactly the same experience as sharing an article from Google Newsstand on Android 5.0.

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, ...);
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, getString(R.string.item_share)));
Categories
discuss

How can I read an AWS S3 File with Java?

I tried to read a file from AWS S3 to my java code:

  File file = new File("s3n://mybucket/myfile.txt");
  FileInputStream fileInput = new FileInputStream(file);

Then I got an error:

java.io.FileNotFoundException: s3n:/mybucket/myfile.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)

Is there a way to open/read a file from AWS S3? Thanks a lot!

Answer

The ‘File’ class from Java doesn’t understand that S3 exists. Here’s an example of reading a file from the AWS documentation:

AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());        
S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, key));
InputStream objectData = object.getObjectContent();
// Process the objectData stream.
objectData.close();
Categories
discuss

charts.js straight lines – i cant find a solution

So im using charts.js http://www.chartjs.org/ and im trying to make the lines between 2 dots be straight and not curvy for no apperent reason.

it right now looks like that http://imgur.com/RrdRgAR,N31ajM7#1

and i want it to look like basic algebric graphs should look like the other picture in the above link

current datasets:

                label: "Shop Sales",
                fillColor : "rgba(255, 89, 114, 0.6)",
                strokeColor : "rgba(51, 51, 51, 1)",
                pointColor : "rgba(255, 89, 114, 1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(151,187,205,1)",
                maintainAspectRatio: false,

and

window.onload = function(){
    var ctx = document.getElementById("canvas").getContext("2d");

    window.myLine = new Chart(ctx).Line(lineChartData, {
responsive: true, scaleFontColor: "#FF5972" }

)};

thank you, im searching for this on the web everywhere

Answer

It looks like it’s been changed with v2.0. It is now tension:0. As of this writing the docs state it’s lineTension:0, but that seems incorrect.

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..