我正在使用 Apache Cordova ,而且我遇到了与Promise
对象有关的多平台问题。
目前,我必须实例化这样的承诺:
var promise = new Promise(...) {
//Implementation
}
这很好,但是如果应用程序在 Windows 平台上运行,我必须使用WinJS
。像这样:
var promise = new WinJS.Promise(...) {
//Implementation
}
这导致以下代码:
var promise;
if (cordova.platformId == "windows") {
promise = new WinJS.Promise(...) {
//Implementation
}
}
else {
promise = new Promise(...) {
//Exactly the same implementation as above
}
}
这里的主要问题是我在每个promise中复制了实现,导致两个代码块完全相同。因此,维护起来比较困难。
有没有办法可以基于当前平台实例化正确的Promise
而无需复制代码两次?
答案 0 :(得分:3)
如果<script type="text/javascript" >
function main_map_init (map, options) {
var promise = $.getJSON('{% url "datapotg" %}');
// Download GeoJSON via Ajax
promise.then(function(data) {
var allbusinesses = L.geoJson(data);
var cafes = L.geoJson(data, {
filter: function(feature) {
return feature.properties.rastvalues == "3";
},
pointToLayer: function(feature, latlng) {
return L.marker(latlng, {
}).on('mouseover', function() {
this.bindPopup(feature.properties.model["rastvalues"]).openPopup();
});
}
});
var others = L.geoJson(data, {
filter: function(feature) {
return feature.properties.rastvalues != "3";
},
pointToLayer: function(feature, latlng) {
return L.marker(latlng, {
}).on('mouseover', function() {
this.bindPopup(feature.properties.model["rastvalues"]).openPopup();
});
}
});
map.fitBounds(allbusinesses.getBounds(), {
padding: [50, 50]
});
cafes.addTo(map)
others.addTo(map)
});
}
</script>
不存在,您可以将其分配到Promise
,然后像往常一样使用WinJS.Promise
。
像:
Promise
答案 1 :(得分:3)