How to replace DomRepeatModel.item? - dart

I have the following code snippet that listen on button click event for item on a list created by dom-repeat (Polymer 1.0.0-rc.17). According to polymer.dart change.log, DomRepeatModel.item is now deprecated, although it will properly use the as attribute for now. The [] operator has been added in its place. Anyone knows how to apply it to the code to get the same result?
#reflectable
handlePurchase(event, [_]) {
print('Purchase item');
var model = new DomRepeatModel.fromEvent(event);
ShopItem item = model.item;
print(item.name);
print(item.price);
}

Found the answer, it is simply model['item'].

Related

LazyColumn and mutable list - how to update?

I'm new to Jetpack Compose and I've spent some hours to find how to make a LazyColumn update what I update my list. I've read that it needs to be a immutable list to update LazyColumn, but I can't seem to get it to work.
The code looks like:
#Composable
fun CreateList() {
var myList : List<DailyItem> by remember { mutableStateOf(listOf())}
myList = getDailyItemList() // Returns a List<DailyItem> with latest values and uses mutable list internally
// Function to refresh the list
val onUpdateClick = {
// Do something that updates the list
...
// Get the updated list to trigger a recompose
myList = getDailyItemList()
}
// Create the lazy column
...
}
I have tried several things and either is the list never updated when tapping the update button or only the first item is updated but not the rest of the items in the list. I looked in the documentation and there it says this, but I don't understand it:
Instead of using non-observable mutable objects, we recommend you use
an observable data holder such as State<List> and the immutable
listOf().
How to update the list so the LazyColumn is updated?
Use SnapshotStateList, the list is mutable. Any modification (add, remove, clear, ...) to the list will trigger an update in LazyColumn.
Similar to mutableListOf() (for MutableList) there is mutableStateListOf() to create a SnapshotStateList.
Extention function swapList() just combines clear() and addAll() calls to replace old list with new list.
fun <T> SnapshotStateList<T>.swapList(newList: List<T>){
clear()
addAll(newList)
}
#Composable
fun CreateList() {
val myList = remember { mutableStateListOf<DailyItem>() }
myList.swapList(getDailyItemList()) // Returns a List<DailyItem> with latest values and uses mutable list internally
// Function to refresh the list
val onUpdateClick = {
// Do something that updates the list
...
// Get the updated list to trigger a recompose
myList.swapList(getDailyItemList())
}
// Create the lazy column
...
}
See the basic idea is to get compose treat the list as state. Now that, you are able to achieve using mutableStateOf(initialValue),
Okay, the process is this,.
We create a variable, initialising it as a mutable state of something
Then we assign that variable to the lazy column. It is not necessary to assign it to the items parameter of the column, but that is our use case here. Otherwise, inside the Composable containing the lazy column, you could just type the name of the variable and even that will work since all we want, is compose to get a message that this variable is being read by the Composable.
Back to the question,
We create a variable, say val mList: List<Int> by remember { mutableStateOf (listOf()) }
Lazycolumn{
items(items = mList){
Text(it)
}
}
Button(onClick = { mList = mList + listOf(mList.size())})
Clicking the button adds a new number to the list, which is reflected in the LazyColumn's UI.

How can search a string array contains metod without loop

I have an array of view model. Now I want to check that view model array to contain a word within an array.
public IQueryable<CategorisedPostViewModel> GetRelatedPostbyCategories(string categories)
{
var ctries = categories.Split(',');
var result = GetAllCategoriedPost().**Where(p=>p.CategoryName.Contains(ctries)).**OrderByDescending(c => c.Published);
return result;
}
How can I search that bold portions without loop?
We may assume for simplicity,
p.categoryName="jerry,tom,ema"
and
ctries={"Gates","jerry","Jobs","ema"}
I want to check if any ctries is found on p.categoryName. Please help me. Thanks in advance.
To check if any category name is present in ctries try, Intersect
p.categoryName.Intersect(tries).Any()

toObservable doesn't seem to be working

I'm using Web UI to do observable data binding. Here is the brief snippet of code I'm working with:
import 'dart:html';
import 'dart:json';
import 'package:web_ui/web_ui.dart';
import 'package:admin_front_end/admin_front_end.dart';
//var properties = toObservable(new List<Property>()..add(new Property(1, new Address('','','','','',''))));
var properties = toObservable(new List<Property>());
void main() {
HttpRequest.request('http://localhost:26780/api/properties', requestHeaders: {'Accept' : 'application/json'})
.then((HttpRequest req){
final jsonObjects = parse(req.responseText);
for(final obj in jsonObjects){
properties.add(new Property.fromJsonObject(obj));
}
});
}
In index.html, I bind properties to it's respective property in the template:
<div is="x-property-table" id="property_table" properties="{{properties}}"></div>
In the first snippet of code, I'm populating the observable properties list, but it never reflects in the UI (I've stepped through the code and made sure elements were in-fact being added). If I pre-populate the list (see the commented out line), it does display, so the binding is at least working properly. Am I doing something wrong here?
The problem is most likely that you don't have any variables or types marked as #observable. In lack of observables, Web UI relies on call to watchers.dispatch() in order to update GUI.
You have following options:
1) import watchers library and call dispatch() explicitly:
import 'package:web_ui/watcher.dart' as watchers;
...
void main() {
HttpRequest.request(...)
.then((HttpRequest req){
for(...) { properties.add(new Property.fromJsonObject(obj)); }
watchers.dispatch(); // <-- update observers
});
}
2) mark any field of your x-property-table component as observable, or just the component type, e.g.:
#observable // <-- this alone should be enough
class PropertyTable extends WebComponent {
// as an alternative, mark property list (or any other field) as observable.
#observable
var properties = ...;
NOTE:
when a collection is marked #observable, UI elements bound to the collection are updated only when the collection object itself is changed (item added, removed, reordered), not when its contents are changed (e.g. an object in the list has some property modified). However, as your original properties list is an ObservableList, #observable annotation only serves here as a way to turn on the observable mechanism. Changes to the list are queued as a part of ObservableList implementation.
I think solution 2 (#observable) is better. As far as I know, watchers is the old way to track changes and will probably be removed.

Jquery class selector is not working

I have following html tags.
<span class="ui-btn-text">ALFL</span>
<span class="ui-btn-text">AL</span>
I want to replace ALFL with
<span class="ui-btn-text">A</span>
through jquery.
I have used
$("span .ui-btn-text").replaceWith("<span class="ui-btn-text">A</span>");
But it is not working. Please give me some answers.
Thanks in advance.
Your selector matches an element that has the class ui-btn-text with a parent that is a span. You just need to remove the space.
I also noticed that your replaceWith replaces the element with a duplicate of itself with some content. There's no need to replace the element, just set the text:
$("span.ui-btn-text").text("A");
What's about
$("span.ui-btn-text").replaceWith("<span class='ui-btn-text'>A</span>");
or
$("span.ui-btn-text").text("A");
If you just want to change the ALFL Text , you can do something like this :
var $span= $('span.ui-btn-text');
$span.each(function(){
if ($(this).text() =='ALFL') {
$(this).text('A');
}
});
Js fiddle Demo : http://jsfiddle.net/95CyN/
If you want to change text/html of all span with class ui-btn-text, do this:
// change the text inside the element
$('span.ui-btn-text').text('A');
OR
// change the html content inside the element
$('span.ui-btn-text').html('A');
OR
// Replace the html element
$('span.ui-btn-text').replaceWith('<span class="ui-btn-text">A</span>');
Your selector is looking for an element with the class of .ui-btn-next inside a span element, because you includied a superfluous space character:
$("span.ui-btn-text").replaceWith("<span class="ui-btn-text">A</span>");
Should be what you want. Also, if you only need to change the content of the element (i.e. to "A"), use text() or html():
$("span.ui-btn-text").text('A');
$("span.ui-btn-text").replaceWith('<span class="ui-btn-text">A</span>');
The replacement string you wrote is going to make the Javascript engine crash.
Try this code friend:-
$("span.ui-btn-text").each(function () {
if ($(this).html() == "ALFL") {
$(this).html("A");
}
});
});

How do you pass statements through an addEventListener?

Iv been trying to pass arguments through an addEventListener event in actionscript such as...
target.addEventListener("pComp", rakeSoil(target));
but i get errors.
Iv tried to google but no luck :/
Thanks for replying if you do :)
The target is already passed as part of the event, either event.currentTarget or event.target will be what you want.
If you want something else passed, create a custom event. Add the property to the custom event.
Try adding an additional method as your event listener:
target.addEventListener ("pComp", targetListener);
...
private function targetListener (event:Event):void {
rakeSoil (event.currentTarget);
}
How this is what you want:
{
var target:EventDispatcher = ...;
Function rakeSoil = function (e:Event):void
{
// handle target
}
target.addEventListener("pComp", rakeSoil);
}
rakeSoil is a first class function(or closure), when event is dispatched, it will be invoked, and you can access 'target' in it.
EDIT:
Have a look at Closure (computer science)
I have always found anonymous functions to be more trouble than they are worth. I would simply follow the standard event handler code layout. It's more formal and takes a little more effort up front, but there is no ambiguity and it is far more readable when you return to it a year from now (reduces head-scratching duration):
// Target extends EventDispatcher
private var target:Target;
public function listenToTarget();
{
target = new Target();
target.addEventListener("pComp", pCompHandler);
}
private function pCompHandler(event:Event):void
{
target.rakeSoil();
}
Although, now that I look at it more closely, why are you having this object do something that Target should be capable of handling internally on its own?

Resources