Given:
swiper =
div []
[ node "swiper-node"
[ class "block" ]
[ div [ id "swiperContainer" ]
[ div [ class "swiper-wrapper" ]
[ div [ class "swiper-slide", style [ ( "height", "400px" ), ( "background", "gray" ) ] ] [ text "slide1" ]
, div [ class "swiper-slide", style [ ( "height", "400px" ), ( "background", "gray" ) ] ] [ text "slide2" ]
, div [ class "swiper-slide", style [ ( "height", "400px" ), ( "background", "gray" ) ] ] [ text "slide3" ]
, div [ class "swiper-slide", style [ ( "height", "400px" ), ( "background", "gray" ) ] ] [ text "slide4" ]
]
]
]
]
<dom-module id="swiper-node">
<template></template>
<style>
</style>
<script>
(function () {
Polymer({
is: 'swiper-node',
});
}());
</script>
</dom-module>
The rendered HTML is:
Notice the empty swiper-node.
The interesting thing is that this happens when the app starts AND swiper is IN the Elm dom.
However, if
the app starts
AND the swiper is NOT in the elm dom
AND on some user event, the swiper is added to the elm dom
it's rendered correctly.
After going through this thread, I did the same configuration of:
window.Polymer = {
dom: 'shadow',
lazyRegister: true
};
And in both cases:
when app starts with swiper
when swiper is added later on user event (click)
this is the rendered html:
Altho we've reached consistency, the swiper-node is not being rendered. U know how when you hover a dom element in the inspector, it highlights in the render window? Well, hovering on any of the swiper-node children, shows nothing.
I'm only utilizing Polymer abstraction/component so i can utilize a js library within Elm in a nice way. So don't assume great Polymer or web-components knowledge on my end.
Just add content inside the template so that the component becomes:
<dom-module id="swiper-node">
<template><content></content></template>
<style>
</style>
<script>
(function () {
Polymer({
is: 'swiper-node',
});
}());
</script>
</dom-module>
::curl_up_and_cry::
Related
I try to pass a JSON as String from my Spring Controller to the React Frontend.
JSON:
{
"name": ".",
"size": 0,
"children": [
{
"name": "setup.bat",
"size": 6,
"children": []
},
{
"name": "src",
"size": 0,
"children": [
{
"name": "main",
"size": 0,
"children": [
{
"name": "java",
"size": 0,
"children": [
...
I try it with Thymeleaf and the console output on my site worked:
index.html
$( document ).ready(function() {
var jsondata = /*[[${jsondata}]]*/;
console.debug(jsondata);
});
but now i try to used it in react I get an error:
app.js:
class App extends Component {
render() {
return(
<div>
{jsondata}
</div>
)
}
}
Uncaught ReferenceError: jsondata is not defined
What is the Problem in React ?
Set and access the JSON on directly on the window object.
window.jsondata = /*[[${jsondata}]]*/;
class App extends Component {
render() {
return(
<div>
{window.jsondata}
</div>
)
}
}
Solution:
I can use the Javascript var with my React code, but the Problem was in my html file. I make a wrong order of the scripts.
If i use the React bundle at the end it works:
index.html:
<script th:inline="javascript">
var jsondata = /*[[${jsonHierarchy}]]*/;
console.log(jsondata);
</script>
<script src="/bundle.js"></script>
I want to access a property, defined in ready() of my Polymer element (as seen in the following code):
Polymer({
is: 'my-list',
ready: function() {
this.tasks = [{
"task": {
"name": "OTS",
"rules": [{"name": "rule 1", "id": "1"}]
}
}];
this.parseJson();
},
parseJson: function() {
this.taskname = JSON.parse(this.tasks.task.name); // errors here
}
});
But I am getting the following error:
Uncaught TypeError: Cannot read property 'name' of undefined
for this line:
JSON.parse(this.tasks.task.name);
How do I fix this?
this.tasks is an array of objects, but parseJson() is not using the correct syntax to access array elements.
parseJson() should be using this.tasks[0].task.name, assuming your actual code can have more than one task and that you're only interested in the first one. Also, you don't need to use JSON.parse(), since the task name is not a JSON string.
Here's a working demo:
Polymer({
is: 'my-list',
ready: function() {
this.tasks = [{
"task": {
"name": "Task1",
"rules": [{
"name": "rule 1",
"id": "1",
}]
}
}];
this.parseJson();
},
parseJson: function() {
this.taskname = this.tasks[0].task.name;
console.log(this.taskname);
}
});
<head>
<base href="https://polygit.org/polymer+:master/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
</head>
<body>
<my-list></my-list>
</body>
I am running this code and getting nothing in output. There is no error in the buildup but I am not able to find out the mistakes. Can anyone tell me what could be the issue?? Regards
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="Dependencies/Angular/angular.js"></script>
<script src="Dependencies/D3/d3.js"></script>
<script src="Dependencies/nvd3/nv.d3.js"></script>
<script src="Dependencies/Angularjs-nvd3-Directives/angularjs-nvd3-directives.js"></script>
<link rel="stylesheet"href="Dependencies/nvd3/nv.d3.css">
<script>
var app = angular.module("nvd3TestApp", ['nvd3ChartDirectives']);
function ExampleCtrl($scope){
$scope.exampleData = [
{
"key": "Series 1",
"values": [ [ 1025409600000 , 0] , [ 1309406400000 , 121.92388706072] , [ 1312084800000 , 116.70036100870] , [ 1314763200000 , 88.367701837033] , [ 1317355200000 , 59.159665765725] , [ 1320033600000 , 79.793568139753] , [ 1322629200000 , 75.903834028417] , [ 1325307600000 , 72.704218209157] , [ 1327986000000 , 84.936990804097] , [ 1330491600000 , 93.388148670744]]
}];
}
</script>
</head>
<body ng-app='nvd3TestApp'>
<div ng-controller="ExampleCtrl">
<nvd3-line-chart data="exampleData"
showXAxis="true"
showYAxis="true"
tooltips="true"
interactive="true">
</nvd3-line-chart>
</div>
</body>
</html>
I believe I have solved your issue with your code. I didn't load any css so you may have to add yours to make it look right. I would also move your script tags to right before the closing </body> tag. That way your code will not run before the dom loads. This will prevent some headaches in the future.
Live Preview: http://codepen.io/larryjoelane/pen/OMQqMd
JavaScript/Angular Code:
var app = angular.module("nvd3TestApp", ['nvd3ChartDirectives']).controller("ExampleCtrl", function($scope) {
$scope.exampleData = [{
"key": "Series 1",
"values": [
[1025409600000, 0],
[1309406400000, 121.92388706072],
[1312084800000, 116.70036100870],
[1314763200000, 88.367701837033],
[1317355200000, 59.159665765725],
[1320033600000, 79.793568139753],
[1322629200000, 75.903834028417],
[1325307600000, 72.704218209157],
[1327986000000, 84.936990804097],
[1330491600000, 93.388148670744]
]
}];
})();
Global functions as controllers haven't been supported in angular for quite a while ... since around 1.2xx
try
var app = angular.module("nvd3TestApp", ['nvd3ChartDirectives']);
app.controller('ExampleCtrl', function($scope) {
$scope.exampleData = [{
"key": "Series 1",
"values": [
......
]
}];
});
In my project I'm binding a list of 'paper-fab' elements from a JSON array.
I want to give only the name of the icon instead of the full path to bind with 'src' property of 'paper-fab'. How can I achieve this? Is it possible to do it with computed property? Thanks in advance.
The code snippet and JSON format is given below.
<ul id="actionButtons">
<template is="dom-repeat" items="[[plugins]]">
<li>
<span>
<paper-fab mini src="[[item.iconSrc]]" id="[[item.id]]" name="[[item.name]]" class$="[[item.cssClass]]"> </paper-fab>
</span>
</li>
</template> </ul>
JSON structure is given below
plugins:
[
{
"id": 1,
"name": "Image_1",
"iconSrc": "app/images/icons/pacs_pull.png",
"cssClass": "white"
},
{
"id": 2,
"name": "Image_2",
"iconSrc": "app/images/icons/pacs_push.png",
"cssClass": "grey"
},
{
"id": 3,
"name": "Image_3",
"iconSrc": "app/images/icons/fileBrowser.png",
"cssClass": "white",
}
]
Check out Computed bindings
<template>
My name is <span>{{computeFullName(first, last)}}</span>
</template>
<script>
Polymer({
is: 'x-custom',
properties: {
first: String,
last: String
},
computeFullName: function(first, last) {
return first + ' ' + last;
}
...
});
</script>
</dom-module>
You should be able to pass item.iconSrc to you compute to append the path to the icon
I have this simple XML View:
<core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m"
controllerName="listicons.list" xmlns:html="http://www.w3.org/1999/xhtml">
<Page title="Title">
<content>
<List id="test-list"></List>
</content>
</Page>
</core:View>
In my controller, I call a method to build list items onInit. First of all set some data:
var data = {
"products": [
{
"prodName": "Apple",
"prodCountry": "Netherlands",
"price": "normal"
},
{
"prodName": "Orange",
"prodCountry": "Spain",
"price": "extra"
},
{
"prodName": "Strawberry",
"prodCountry": "Poland",
"price": "normal"
}
]
};
// create a Model with this data and attach it to the view
var model = new sap.ui.model.json.JSONModel();
model.setData(data);
this.getView().setModel(model);
var list = this.getView().byId("test-list");
Then I build the list and bind the items to it:
// bind the List items to the data collection
list.bindItems({
path : "/products",
sorter : new sap.ui.model.Sorter("prodName"),
//template : listTmpl
template : new sap.m.StandardListItem({
title: "{prodName}",
description: "{prodCountry}"
})
});
After I built the list and is alread rendered, I look after which items have an extra price and set an icon for them:
jQuery.each(list.getItems(), function(i, obj) {
if(obj.mProperties.price == "extra") {
obj.setIcon("sap-icon://flag");
}
});
So. Everything works fine. But I am not happy with my solution, because I'd rather like to manipulate the data BEFORE rendering the list. I tried to build a list template directly before binding the items to the list and then use this template like:
var listTmpl = jQuery.each(data.products, function(i, a) {
var lI = new sap.m.StandardListItem({
title: "{prodName}",
description: "{prodCountry}"
});
if(a.price == "extra") {
lI.setIcon("sap-icon://flag");
}
return lI;
});
But then my list is not shown and I got an error in the console, saying
Missing template or factory function for aggregation items of Element sap.m.List ...
Does anyone have an idea how to improve my sol.?
THX a lot..
IMHO, I think you can have the controller as clean as possible, and define most of the needed functionality (binding, template, sorter, and icon) in the XMLView:
<List id="test-list" items="{
path : '/products',
sorter : [{
path : 'prodName',
descending : true
}]
}">
<StandardListItem title="{prodName}"
description="{prodCountry}"
icon="{path:'price', formatter:'.getIconFlag'}" />
</List>
You then can rid of all the template binding and manipulation stuff you have in your controller, and you only need to specify the formatter function getIconFlag:
getIconFlag : function (sPrice) {
return sPrice === "extra" ? "sap-icon://flag" : null;
}
See the following working example:
sap.ui.controller("view1.initial", {
onInit : function(oEvent) {
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData({
"products": [
{
"prodName": "Apple",
"prodCountry": "Netherlands",
"price": "normal"
},
{
"prodName": "Orange",
"prodCountry": "Spain",
"price": "extra"
},
{
"prodName": "Strawberry",
"prodCountry": "Poland",
"price": "normal"
}
]
});
this.getView().setModel(oModel);
},
getIconFlag : function (sPrice) {
return sPrice === "extra" ? "sap-icon://flag" : null;
}
});
sap.ui.xmlview("main", {
viewContent: jQuery("#view1").html()
})
.placeAt("uiArea");
<script id="sap-ui-bootstrap"
src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
data-sap-ui-theme="sap_bluecrystal"
data-sap-ui-xx-bindingSyntax="complex"
data-sap-ui-libs="sap.m"></script>
<div id="uiArea"></div>
<script id="view1" type="ui5/xmlview">
<mvc:View
controllerName="view1.initial"
xmlns="sap.m"
xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc" >
<List id="test-list" items="{
path: '/products',
sorter: [{
path: 'prodName',
descending: true
}]
}">
<StandardListItem title="{prodName}" description="{prodCountry}" icon="{path:'price', formatter:'.getIconFlag'}" />
</List>
</mvc:View>
</script>
bind the value for price to a formatter-function. so you can create the icons dynamically from the value
list.bindItems({
path : "/products",
sorter : new sap.ui.model.Sorter("prodName"),
template : new sap.m.StandardListItem({
title: "{prodName}",
description: "{prodCountry}",
/* bind items to factory-function */
icon: {
path: "price",
formatter: function(price) {
if (price == "extra") {
return "sap-icon://flag";
}
}
}
})
});
ps: i did not test this, but it should work like this. if you receive errors just comment.