ReactiveUI - ReactiveCommand to get more data for objects in ReactiveList - reactiveui

Currently I am learning ReactiveUI and I am not sure how to approach this problem. What I want to achieve is once a reactive list has been loaded (in this case I am using a command to load it from a local store) I want to be able to trigger that each of the items in the reactive list then go off and fetch data from an api endpoint and update the view model.
What I currently have to load and create the view models using this logic:
LoadSavedLocations = ReactiveCommand.CreateAsyncTask(async o => {
var savedLocations = await _savedLocationService.GetUserSavedLocations();
return savedLocations;
});
LoadSavedLocations.Subscribe(savedLocations =>
{
foreach (var savedZone in savedLocations)
{
Zones.Add(new ZoneDetailViewModel() {
ZoneId = savedZone.ZoneId,
SavedName = savedZone.SavedName,
});
}
});
I want to then be able to have a command that I can kick off (one first load of the screen and then when the user prompts for an update - pull for reload).
There are two ways I think I can do this but struggling with the approach and the code to achieve this.
Option 1
A command which loops through the items in the ReactiveList fetches data from the Api and then updates that viewmodel something along the lines of
UpdateZones = ReactiveCommand.CreateAsyncTask(async o =>
{
foreach (var zone in Zones)
{
// Fetch
// Await
// Update view model
}
return null;
});
With this I am confused around what the return type of the command would be just a new object()? Is there a better way than just looping like this?
Option 2
On the view model ZoneDetailViewModel have a command called FetchExtraData which will then return the data from the API and I can subscribe to that command in the view model and populate the extra properties. With this approach how does the parent viewmodel trigger all the items in the ReactiveList to fire their commands.
For both approaches I don't know how to get each of the items in the ReactiveList to do logic which involves going to an Api and update.

Related

Xamarin c#: Parse.com relation table seems to only work one way?

I'm working with Xamarin in Visual Studio.
I'm utilizing Parse (via SashiDo.com) and I'm trying to create a relation between my Users and the ParseObjects in a table called called Dispatch, like so:
//Make the new Dispatch object
var parseDispatch = new Parse.ParseObject("Dispatch");
//Save it
await parseDispatch.SaveAsync();
//...Setting various properties on the Dispatch object...
//Get a list of users (via another method)
IEnumerable<ParseUser> usersToLink = UsersToLinkToDispatch(); //And have verified elsewhere that this indeed returns a collection of ParseUsers
//Go through the users collection
usersToLink.ToList().ForEach( async (user) => {
//Get or create the dispatches-tracking relation for this user
var dispatchObjectRelation = parseDispatch.GetRelation<ParseObject>("DispatchesTracked");
//Add the current user to that tracker relation
dispatchObjectRelation.Add(user);
//save the dispatch to update the relation
await parseDispatch.SaveAsync();
});
So, when I go to inspect my tables in SashiDo, if I look at the Dispatches table, I see a proper-looking relational link, and if I click on that link, I see the list of linked Users. So far so good, right?
But if I look at the Users table, while there also seems to be a proper-looking relational link, when I click on it I do not see a list of linked Dispatches.
Is this expected behavior, or is this apparent one-way-ness of the relational link an error?

Manually loading navigation properties in MVC 6

What is the best way to manually load navigation property in MVC 6?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Reservation reservation, bool ignoreConflicts = false)
{
int id = reservation.ItemID; // correct item ID
Item item = reservation.Item; // null
}
In this example, when user submits a form, I get a Reservation object with all navigation properties set to null.
The best way I can think of is manually looking up the Item DbSet to find the item with matching ID and assigning it to the navigation property.
Well, first thing first... you are in an MVC action. What you will receive will be from the client. Not the database directly.
So reservation.Item might not be null but it might not be the real data either.
In this scenario, you first have to retrieve your data from the server and then do one of two things:
Update the EF7 entity directly by hand from what you received from the client
Use TryUpdateModelAsync to update your current entity automatically.
It could be as simple as:
public ActionResult Update(Reservation reservation)
{
var item = context.Items.FirstOrDefault(x => x.ID == reservation.ItemID);
await TryUpdateModelAsync(item);
// todo: save context
// return....
}
This is an Entity Framework 7 issue, not an MVC6 one.
Lazy loading isn't implemented for EF7 yet, possibly it won't be at all.
You can track the ticket here https://github.com/aspnet/EntityFramework/issues/3797
This means instead we need to do it as you suggested; something like:
Item item = context.Items.FirstOrDefault(x => x.ID == reservation.ItemID);

In Meteor, where do I model my business rules?

Beginner question : I've worked through the Try Meteor tutorial. I've got fields in my HTML doc, backed by helper functions that reference collections, and BOOM --> the fields are updated when the data changes in the DB.
With the "Hide completed" checkbox, I've also seen data-binding to a session variable. The state of the checkbox is stored in the Session object by an event handler and BOOM --> the list view is updated "automatically" by its helper when this value changes. It seems a little odd to be assigning to a session object in a single page application.
Through all this, my js assigns nothing in global scope, I've created no objects, and I've mostly seen just pipeline code, getting values from one spot to another. The little conditional logic is sprayed about wherever it is needed.
THE QUESTION... Now I want to construct a model of my business data in javascript, modelling my business rules, and then bind html fields to this model. For example, I want to model a user, giving it an isVeryBusy property, and a rule that sets isVeryBusy=true if noTasks > 5. I want the property and the rule to be isolated in a "pure" business object, away from helpers, events, and the meteor user object. I want these business objects available everywhere, so I could make a restriction, say, to not assign tasks to users who are very busy, enforced on the server. I might also want a display rule to only display the first 100 chars of other peoples tasks if a user isVeryBusy. Where is the right place to create this user object, and how do I bind to it from my HTML?
You can (and probably should) use any package which allows you to attach a Schema to your models.
Have a look at:
https://github.com/aldeed/meteor-collection2
https://github.com/aldeed/meteor-simple-schema
By using a schema you can define fields, which are calculated based on other fields, see the autoValue property: https://github.com/aldeed/meteor-collection2#autovalue
Then you can do something like this:
// Schema definition of User
{
...,
isVeryBusy: {
type: Boolean,
autoValue: function() {
return this.tasks.length > 5;
}
},
...
}
For all your basic questions, I can strongly recommend to read the DiscoverMeteor Book (https://www.discovermeteor.com/). You can read it in like 1-2 days and it will explain all those basic questions in a really comprehensible way.
Best Regards,
There is a very good package to implement the solution you are looking for. It is created by David Burles and it's called "meteor-collection-helper". Here it the atmosphere link:
You should check the link to see the examples presented there but according to the description you could implement some of the functionality you mentioned like this:
// Define the collections
Clients = new Mongo.Collection('clients');
Tasks = new Mongo.Collection('tasks');
// Define the Clients collection helpers
Clients.helpers({
isVeryBusy: function(){
return this.tasks.length > 5;
}
});
// Now we can call it either on the client or on the server
if (Meteor.isClient){
var client = Clients.findOne({_id: 123});
if ( client.isVeryBusy() ) runSomeCode();
}
// Of course you can use them inside a Meteor Method.
Meteor.methods({
addTaskToClient: function(id, task){
var client = Clients.findOne({_id: id});
if (!client.isVeryBusy()){
task._client = id;
Tasks.insert(task, function(err, _id){
Clients.update({_id: client._id}, { $addToSet: { tasks: _id } });
});
}
}
});
// You can also refer to other collections inside the helpers
Tasks.helpers({
client: function(){
return Clients.findOne({_id: this._client});
}
});
You can see that inside the helper the context is the document transformed with all the methods you provided. Since Collections are ussually available to both the client and the server, you can access this functionality everywhere.
I hope this helps.

Knockout Mapping - Fill Observable Arrays keeping Items' methods

I've been facing a problem that is basically the following:
I have a knockout ViewModel which contains observable arrays of items with observable properties and methods.
I need to pull data from the server. The methods need to exist after data is taken from server. So I create a new ViewModel and then update its value from what comes from server. (THIS DOES NOT WORK, THE RESULTING ARRAY HAS NO ITEMS)
If I create, with mapping, a new object using var newObj = ko.mapping.fromJS(data) the resulting Array has items, but its items have no methods. It spoils my Bindings.
The fiddle of my problem: http://jsfiddle.net/claykaboom/R823a/3/ ( It works util you click in "Load Data From The Server" )
The final question is: What is the best way to have items on the final array without making the loading process too cumbersome, such as iterating through every item and filling item's properties in order to keep the previously declared methods?
Thanks,
I changed your code little bit. Check this version of JSFiddle.
var jsonFromServer = '{"ModuleId":1,"Metadatas":[{"Id":1,"MinValue":null,"MaxValue":null,"FieldName":"Teste","SelectedType":"String","SelectedOptionType":null,"IsRequired":true,"Options":[]}]}';
Your code doesnt work because your jsonFromServer variable does not contain methods we need at binding like you described in your question. ( -- > Metadatas )
So we need to define a custom create function for Metadata objects at the mapping process like this :
var mapping = {
'Metadatas': {
create: function(options) {
var newMetaData = new MetadataViewModel(options.parent);
newMetaData.Id(options.data.id);
newMetaData.FieldName(options.data.FieldName);
newMetaData.SelectedType(options.data.SelectedType);
newMetaData.SelectedOptionType(options.data.SelectedOptionType);
newMetaData.IsRequired(options.data.IsRequired);
newMetaData.Options(options.data.Options);
// You can get current viewModel instance via options.parent
// console.log(options.parent);
return newMetaData;
}
}
}
Then i changed your load function to this :
self.LoadDataFromServer = function() {
var jsonFromServer = '{"ModuleId":1,"Metadatas":[{"Id":1,"MinValue":null,"MaxValue":null,"FieldName":"Teste","SelectedType":"String","SelectedOptionType":null,"IsRequired":true,"Options":[]}]}';
ko.mapping.fromJSON(jsonFromServer, mapping, self);
}
You dont have to declare a new viewModel and call ko.applyBindings again. Assigning the updated mapping to current viewModel is enough. For more information check this link. Look out for customizing object construction part.
The final question is: What is the best way to have items on the final
array without making the loading process too cumbersome, such as
iterating through every item and filling item's properties in order to
keep the previously declared methods?
As far as i know there is no easy way to do this with your object implemantation. Your objects are not simple. They contains both data and functions together. So you need to define custom create function for them. But if you can able to separate this like below then you dont have to customize object construction.
For example seperate the MetadataViewModel to two different object :
--> Metadata : which contains only simple data
--> MetadataViewModel : which contains Metadata observableArray and its Metadata manipulator functions
With this structure you can call ko.mapping.fromJSON(newMetaDataArray , {} , MetadataViewModelInstance.MetadataArray) without defining a custom create function at the mapping process.

reading related data after a selection of a foreign key - MVC3 and EF4

I am new to MVC and EF and I have a question.
I have built a site with models views controllers etc.
On an edit view for a Case (pretty big model so I won't post it here) I have a FK to a Customer model using CustomerID. When a user selects a customer id from a drop down list, I would like to display CustomerName, CustomerPhone etc after the selection of the ID. I think I might need to do a post back for this to work?
Also, do I need to Include the related entities as part of the initial data "get"? I have read some detail on that but I dont fully understand how that needs to work.
Please let me know if I should post more info. Thanks!
Here is my ActionResult for Edit
public ActionResult Edit(int id)
{
Cases cases = db.Cases.Find(id);
//related data needs to loaded to show related data fields
//include related data entities
var v = db.Cases.Include("Customers");
ViewBag.TechnicianID = new SelectList(db.Technicians, "TechnicianID", "LastName", cases.TechnicianID);
ViewBag.BranchID = new SelectList(db.Branches, "BranchID", "BranchName", cases.BranchID);
ViewBag.EngineModelID = new SelectList(db.EngineModels, "EngineModelID", "EngineModelName", cases.EngineModelID);
ViewBag.CaseCategoryID = new SelectList(db.CaseCategories, "CaseCategoryID", "CategoryName",cases.CaseCategoryID);
ViewBag.Qualified = new SelectList(new[] { "YES", "NO", "PARTIALLY" });
ViewBag.CaseStatus = new SelectList(new[] { "OPEN/IN PROCESS", "CLOSED" });
return View(cases);
}
The line
var v = db.Cases.Include("Customers")
is what I am trying to use to load related customer data and then show in my edit view like this:
#Html.EditorFor(model => model.Customer.CustomerName)
Well it depends on what you are trying to do. You could include a model which holds all the required data and send it with every call on that page (initial empty ofcourse)
Once you selected the customer, do post-back and send the customerId to your application and return the same page with the desired model.
You could do that via AJAX too.
Also, do I need to Include the related entities as part of the initial data "get"?
Not sure if I understand what you are trying to say. You mean that you think you would have to send all customer's data down to the page and select the related data on client side?

Resources