Categories
discuss

jQuery DOM manipulation with each();

I’m trying to do some simple DOM manipulation on several elements at once with jQuery using each(). I’m getting results that I don’t understand.

Here is a jsFiddle that shows what I want to happen VS what actually happens:

http://jsfiddle.net/kthornbloom/4T52A/2/

And here is the JS:

// Step One: Append one blue box within each grey box

$('.grey').append('<div class="blue"></div>');

// Step Two: Make one copy of the red box already there, and place it within the new blue box.

$('.grey').each(function () {
    $('.red', this).clone().appendTo('.blue', this);
});

Why am I getting the results I am, and how can I acheive the desired results?

Answer

That is because context selector doesnt work in the .append(). The fastest solution (not optimal) is to recreate a new jQuery object :

$('.red', this).clone().appendTo($('.blue', this));

Fiddle : http://jsfiddle.net/4T52A/3/

Here an optimal solution :

$('.grey').each(function () {
    var $this = $(this);
    $this.find('.red').clone().appendTo($this.find('.blue'));
});
Categories
discuss

Can I prevent panning Leaflet map out of the world’s edge?

Is there a way to limit panning out of the world’s edge? On this picture, brown is the world, grey is emptiness. I want to make it impossible to pan like this.

out of this world

Answer

Leaflet allows you to control how much the map resists being dragged out of bounds with the maxBoundsViscosity option (value: 0 to 1). Setting it to maximum disables dragging out of bounds entirely.

var map = new L.Map('map', {
  center: bounds.getCenter(),
  zoom: 5,
  layers: [osm],
  maxBounds: bounds,
  maxBoundsViscosity: 1.0
});

This feature is available in 1.0.0. The relevant pull request includes a working example:

var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
  osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
  osm1 = L.tileLayer(osmUrl, {
    maxZoom: 18,
    attribution: osmAttrib
  }),
  osm2 = L.tileLayer(osmUrl, {
    maxZoom: 18,
    attribution: osmAttrib
  }),
  bounds = new L.LatLngBounds(new L.LatLng(49.5, -11.3), new L.LatLng(61.2, 2.5));

var map1 = new L.Map('map1', {
  center: bounds.getCenter(),
  zoom: 5,
  layers: [osm1],
  maxBounds: bounds,
  maxBoundsViscosity: 0.75
});

var map2 = new L.Map('map2', {
  center: bounds.getCenter(),
  zoom: 5,
  layers: [osm2],
  maxBounds: bounds,
  maxBoundsViscosity: 1.0
});

var latlngs = L.rectangle(bounds).getLatLngs();
L.polyline(latlngs[0].concat(latlngs[0][0])).addTo(map1);
L.polyline(latlngs[0].concat(latlngs[0][0])).addTo(map2);
html,
body,
#map {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.0/dist/leaflet.js"></script>

<h1>Left: Bouncy maxBounds. Right: Not bouncy.</h1>

<div id="map1" style="float: left; width:45%; height: 80%;"></div>
<div id="map2" style="float: left; width:45%; height: 80%;"></div>
Categories
discuss

How would I get a File object from PhoneGap camera.getPicture?

This is probably simple and covered by some combination of functions in PhoneGap’s “Camera” plugin, “File” plugin, or “File-Transfer” plugin. I understand the user can select a file with:

navigator.camera.getPicture(function (fileURI) {

    // *** need help here ***

}, function ()
    // handle errors
}, {
    destinationType: window.Camera.DestinationType.FILE_URI,
    sourceType: window.Camera.PictureSourceType.PHOTOLIBRARY,
    mediaType: window.Camera.MediaType.ALLMEDIA
});

I can also change to destinationType: window.Camera.DestinationType.DATA_URL if that makes a difference.

My goal in the success handler is to get a File object (https://developer.mozilla.org/en-US/docs/Web/API/File).

Answer

Something like this should do it.

navigator.camera.getPicture(function (fileURI) {

    window.resolveLocalFileSystemURL(fileURI, 
        function(fileEntry){
            alert("got image file entry: " + fileEntry.fullPath);
            // fileEntry.file() should return a raw HTML File Object
        },
        function(){//error}
    );

}, function (){
// handle errors
}, {
    destinationType: window.Camera.DestinationType.FILE_URI,
    sourceType: window.Camera.PictureSourceType.PHOTOLIBRARY,
    mediaType: window.Camera.MediaType.ALLMEDIA
});
Categories
discuss

How to call a scope inside a ng-repeat just for the last element?

I want to call a scope function inside my ng-repeat just for the last element, like this:

<div ng-repeat="patient in patients" >
  <div ng-if="$last" ng-load="myFunction()"></div>
</div>

But ng-load doesn’t work!

Answer

I think this should work:

  <div ng-repeat="element in array" ng-init="$last && myFunction(element)">
       {{element}}          
  </div>

There you have working JSFiddle.

Categories
discuss

Specify number of partitions on Kafka producer

I have the following code from the web page https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+Producer+Example

What seems to be missing is how to configure the number of partitions. I want to specify 4 partitions but am always ending up with the default of 2 partitions. How do I change the code to have 4 partitions(without changing the default).

    Properties props = new Properties();
     
    props.put("metadata.broker.list", "localhost:9092,broker2:9092");
    props.put("serializer.class", "kafka.serializer.StringEncoder");
    props.put("partitioner.class", "com.gnip.kafka.SimplePartitioner");
    props.put("request.required.acks", "1");
    props.put("num.partitions", 4);
     
    ProducerConfig config = new ProducerConfig(props);
    Producer<String, String> producer = new Producer<String, String>(config);
    
    Random rnd = new Random();
    for (long nEvents = 0; nEvents < 1000; nEvents++) { 
        long runtime = new Date().getTime();
        String ip = "192.168.2." + rnd.nextInt(255);
        String msg = runtime + ",www.example.com," + ip;
        KeyedMessage<String, String> data = new KeyedMessage<String, String>("page_visits2", ip, msg);
        producer.send(data);
    }

    producer.close();

Answer

The Kafka producer api does not allow you to create custom partition, if you try to produce some data to a topic which does not exists it will first create the topic if the auto.create.topics.enable property in the BrokerConfig is set to TRUE and start publishing data on the same but the number of partitions created for this topic will based on the num.partitions parameter defined in the configuration files (by default it is set to one).

Increasing partition count for an existing topic can be done, but it’ll not move any existing data into those partitions.

To create a topic with different number of partition you need to create the topic first and the same can be done with the console script that shipped along with the Kafka distribution. The following command will allow you to create a topic with 2 partition (as specified by the --partition flag)

    bin/kafka-create-topic.sh --zookeeper localhost:2181 --replica 3 --partition 2 --topic my-custom-topic

Unfortunately as far as my understanding goes currently there is no direct alternative to achieve this.

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