Do I have two instances of the same store? - store

i populate a listview using a store with jsonp proxy that sends a GET request to my server. when the app launches the request is send and the list items are rendered with the response as they should. now when i then manually add a new item in my data-base server and refresh the list with the code shown in the controller below and inspect the store object in the console i can see that the newly created item is in the store data. so far so good, everything as expected. however, the new item is not in the refreshed in the list. the strange thing is that when i inspect the list object and its containing store object the newly created item is not included. However when i relaunch the app (rather than reload the store and refresh the list) new newly created item appears in the list. to me it seems that somehow i have 2 store instances, one with the new data (which i retreive with Ext.getStore) and one without (the one inside the listview) which i thought should be actually the same. how is that possible? below i show you how i do the thing. Any help is greatly appreciated.
the controller
Ext.define('VB1.controller.ListController', {
extend: 'Ext.app.Controller',
config: {
refs: {
listView: 'listview',
refreshButtonTap: '#refreshbutton',
},
control: {
refreshButtonTap: {
onRefreshList: 'refreshList',
},
}
},
launch: function () {
this.callParent(arguments);
Ext.getStore('MyStore').addListener('load', 'recordLoaded', this);
},
refreshList: function () {
var loadedStore = Ext.getStore('MyStore').load();
console.log(loadedStore) //here the new item is included
},
recordLoaded: function () {
var list = this.getListView();
list.refresh();
console.log(list) //here the new item is not included in the store object inside
//listobject
}
});
the Store
Ext.define('VB1.store.MyStore', {
extend: 'Ext.data.Store',
alias: 'widget.mystore',
config: {
model: 'VB1.model.MyModel',
autoLoad: true,
proxy: {
type: 'jsonp',
url : 'myUrl.js',
enablePagingParams: false,
reader: {
type: 'json',
rootProperty: 'array.resources'
},
},
},
});
the listview
Ext.define('VB1.view.ListView', {
extend: 'Ext.dataview.List',
alias: 'widget.listview',
requires: [
'Ext.dataview.List',
],
scrollable: 'vertical',
config: {
loadingText: "loading...",
emptyText: '</pre><div class="notes-list-empty-text">No notes found.</div><pre>',
itemTpl: '</pre><div class="list-item-title">{title}</div><div class="list-item-narrative"><img src="{listlogo}"/></div><pre>',
},
listeners: {
itemtap: function(list, index, item, record) {
this.fireEvent('showDetails', record, this);
}
},
});
this is how i add the list including the store to the viewport at app launch
var listView = {
xtype: 'listview',
store: {xtype: 'mystore'},
}
Ext.Viewport.add(listView);

I just found out what the problem was. first of all the store was instantiated 2 times as I suspected. obviously one time by the app and another time by the view. hence i ended up with having two stores. since i did not set a storeID the stores became identified by two unique ids based on the xtype name i have given in the alias config of the store.
the store i resolved and logged in the controller like below had the id ex-mystore-1
var loadedStore = Ext.getStore('MyStore').load();
console.log(loadedStore) //here the new item was included
and the store inside the list view object got the id ext-mystore-2
var list = this.getListView();
console.log(list._store); //here the new item was not included
what changed the hole problem in my favor was when i added a storeId to the store. after that i still had two stores, however, with the same storeIds (but with with different unique or observable ids). the good new was that now the new item was included in both store instances. i also was able to get rid of the second store instance by not instantiating the xtype of the store in the listview but reference it by storeID. Now everything works as it should. thanks to anybody who looked at this. i hope this helpes somebody who finds similar trouble.

Related

How to build proposed order with what the user has selected?

I'm building an AOG (actions on google) project that will do basic transaction functionality. Since I'm still a bit new to AOG, I'm completely stuck on how to take what the user selects (whether it be a carousel, a basic card etc.) and pass that argument value/key that they selected into the proposed order or the order preview before they finish their transaction.
Here is basically what I have tried (This isn't the actual code because it's rather long, but it still gets the idea across)
app.intent('delivery_address_complete', (conv) => {
const arg = conv.arguments.get('DELIVERY_ADDRESS_VALUE');
if (arg.userDecision ==='ACCEPTED') {
conv.ask('Ok, what would you like to order?');
conv.ask(new Suggestions(intentSuggestions));
conv.ask(new Carousel({
items: {
// Add the first item to the carousel
SELECTION_KEY_COFFEE: {
synonyms: [
'Coffee'
],
title: 'Coffee',
description: 'Sweet cream and sugar coffee.',
image: new Image({
url: IMG_URL_COFFEE,
alt: 'Image alternate text',
}),
},
}));
}
});
const yesOrno = [
'Yes',
'No'
];
app.intent('actions.intent.OPTION', (conv ) => {
conv.ask('Okay, are you ready to proceed?');
conv.ask(new Suggestions(yesOrno));
});
app.intent('transaction_decision_action', (conv) => {
const order = {
id: UNIQUE_ORDER_ID,
cart: {
merchant: {
id: 'coffee',
name: 'Coffee Store',
},
lineItems: [
{
name: 'My Memoirs',
id: 'coffee_1',
price: {
amount: {
currencyCode: 'USD',
nanos: 990000000,
units: 3,
},
type: 'ACTUAL',
},
quantity: 1,
subLines: [
{
note: 'coffee',
},
],
type: 'REGULAR',
},
otherItems: [
{
name: 'Subtotal',
id: 'subtotal',
price: {
amount: {
currencyCode: 'USD',
nanos: 220000000,
units: 32,
},
type: 'ESTIMATE',
},
type: 'SUBTOTAL',
},
{
name: 'Tax',
id: 'tax',
price: {
amount: {
currencyCode: 'USD',
nanos: 780000000,
units: 2,
},
type: 'ESTIMATE',
},
type: 'TAX',
},
],
totalPrice: {
amount: {
currencyCode: 'USD',
nanos: 0,
units: 35,
},
type: 'ESTIMATE',
},
};
Please note: This is mostly dummy code, so if some things like over charging or prices not making sense is happening, it's not the problem I'm trying to fix.
How can I take what the user selected from whatever method, and get it so it will appear on the order preview or proposed order? I do not need help with anything regarding making carousels or basic cards ect. Just how to get this selected information to the order preview.
To be more specific:
I can create an order object that is required, and I know how to send it to Google (and then to the user) as part of a ProposedOrder object that becomes part of the TransactionDecision object. (The "transaction_decision_action" Intent handler in the code above.)
What I don't understand is how to build the order based on the user saying things or by selecting on carousel or list items that I've shown them. (What do I do in the "actions.intent.OPTION" Intent handler above, for example?)
edit: This also may clear up any confusion. This is a video representation of what I'm attempting to do (mentioned in comments below):
youtube.com/watch?v=LlgMcJBnNN8 from 1:02 to 1:29 I know how to do, I'm confused (In the video example) how they were able to get the 'turkey sandwich' and the 'Green smoothie' added to the order preview at 1:35 ish from the carousel selections
What you're looking to do is what Google refers to as building the order. As it notes at that link
Once you have the user information you need, you'll build a "cart
assembly" experience that guides the user to build an order. Every
Action will likely have a slightly different cart assembly flow as
appropriate for your product or service.
You could build a cart assembly experience that enables the user to
re-order their most recent purchase via a simple yes or no question.
You could also present the user a carousel or list card of the top
"featured" or "recommended" items. We recommend using rich responses
to present the user's options visually, but also design the
conversation such that the user can build their cart using only their
voice.
For more information on how to build a high-quality cart assembly
experience, see the Transactions Design Guidelines.
So there is no one way to do what you're asking about. However, there are a few tips of things you can and should be doing to build the proposed order.
Managing the order
The big thing you need to do is to keep track of all the things that the user is ordering as you go through the process. There are a number of ways you can store this information:
In a Dialogflow Context
In the user session store
In a database or data store for the session
In short, any of the current ways you have to store session information. All of the information below assumes you've picked some way to do this.
Since everything will become one of the lineItems, an easy solution is to build this array as you go along, and then you can just copy the array directly into the order object. Another approach is to just store a list of item IDs, and then populate the rest of the information later when we build the order.
For this example, we're going to go with this latter scheme (because its easier to show) and store it in the session storage object using the actions-on-google library.
So for starters, when we start the Action, or when we know we'll be taking the order, we need to initialize our list of items being ordered with something like
conv.user.data.items = [];
Now that we have our initial item list, we can explore different ways to add to this list.
Adding an item: "my regular"
For some types of orders, it may make sense for the user to be able to say "I'll have my usual". In cases like this, we want an Intent that handles this phrase (or handles a "yes" response to our prompting), and an Intent Handler that looks up the user's regular order and adds it to the items. Perhaps something like this:
app.intent('order.usual', conv => {
// Get their user profile from our database
// The "loadUser" function is up to you, and has little to do with AoG
return loadUser( conv )
.then( user => {
// Add each item in their usual order to the current items
let usualOrder = user.usualOrder;
usualOrder.forEach( item => conv.user.data.items.push( item ) );
// Send a message back to the user
conv.add( "You got it! Do you want anything else?" );
});
});
Adding an item from a list
If you've presented a carousel or a list to the user of possible items, your life is a little easier (although you may not think it at the moment). You do need to setup a Dialogflow Intent that handles the actions_intent_OPTION event (which I'll call order.option in this case).
In the handler for this, we'll assume that the key you used for the option also happens to be the item ID, so you can just add it to the list
app.intent('order.option', (conv, params, option) => {
// The item is the option sent
let item = option;
// Add the item to the list of items
conv.user.data.items.push( item );
// Send a message back to the user
conv.add( "I've put that in your cart. Anything else?" );
});
Adding an item by name
But remember, the user can take the conversation in any direction at any time. So they may ask for an item that you currently aren't showing in the carousel. The best way to handle this is by creating an Entity Type in Dialogflow (which I'll call item, as an example)
And then an Intent that captures some phrases that expresses the user asking to add them (which I'll call order.name and which has an itemName parameter that the user has to include).
[
In the handler, you need to get the name that they spoke, look up what the item is, and add this to the list of items they've ordered.
app.intent('order.name', (conv, params) => {
// Get the name
let itemName = params['itemName'];
// Look it up to find out what they ordered
// You need to implement the itemFromName function
return itemFromName( itemName )
.then( item => {
// Add the item
conv.user.data.items.push( item );
// And reply
conv.add( "You got it! Anything else?" );
});
});
Finish building the order
Once you've finished collecting everything they want, your Intent Handler should put the order together, assembling the full list of lineItems from the conv.user.data.items array that we've been putting together, calculating tax, totals, and all the other parts of the order.
We then need to propose the order by sending a TransactionDecision object that contains our order in the proposedOrder parameter. Clever, no? Possibly something like this:
app.intent('review', conv => {
// Get the items the user has saved
let items = conv.user.data.items;
// Turn these into more complete lineItems
// You will need to provide the "itemToLineItem" function
let lineItems = items.map( itemToLineItem );
// Get some other objects we need
// You'll need to define these functions, too
let orderId = generateOrderId();
let subtotal = computeSubtotal( lineItems );
let tax = computeTax( lineItems );
let total = computerTotal( subtotal, tax );
// Build the order object
let order = buildOrder( lineItems, subtotal, tax, total );
conv.ask(new TransactionDecision({
orderOptions: {
requestDeliveryAddress: false,
},
paymentOptions: {
googleProvidedOptions: {
prepaidCardDisallowed: false,
supportedCardNetworks: ['VISA', 'AMEX'],
// These will be provided by payment processor,
// like Stripe, Braintree, or Vantiv.
tokenizationParameters: {
tokenizationType: 'PAYMENT_GATEWAY',
parameters: {
'gateway': 'stripe',
'stripe:publishableKey': (conv.sandbox ? 'pk_test_key' : 'pk_live_key'),
'stripe:version': '2017-04-06'
},
},
},
},
proposedOrder: order,
}));
});
I broke most of the stuff out as a function since there is nothing specific about them, except the format of the order (which you illustrate in your example). You can really build it any way you want.
Conclusion
Much of what you need to do really boils down to
Collecting the information of what the user wants to order, mostly storing the IDs of these items
Turning this list of items into the complete order object
Sending this order for the user to review

CouchDB/PouchDB Selectors not syncing properly

I have set up a selector and my pouchDB syncs documents based on that selector.
Here is the selector. It will get all documents of type="job" and that include 1371 ID in permission array OR any document of type="load".
const myselector = {
$or: [
{
type: {
$eq: 'job'
},
permission: {
$elemMatch: { $eq: 1371 }
}
},
{
type: {
$eq: 'load'
}
}
]}
Here's how I sync
this.db.replicate
.from(this.remoteDB, {
selector: myselector
})
.on('complete', info => {
// then two-way, continuous, retriable sync
this.db
.sync(this.remoteDB, {
live: true,
retry: true,
selector: myselector
})
.on('change', change => {
console.log('my change0', change);
})
.on('error', error => {
console.log('my error', error);
});
})
.on('error', error => {
console.log('my error2', error);
});
This works perfectly and tells me if theres a change in any document but If e.g I make a change in permissions array and id 1371 gets changed then it doesnot notify me of the change. Now if I change any other field in that document, it doesnot get replicated as its not been synced anymore.
I'm using Selector instead of CouchDB Filters as they're apparently 10x faster but this sync becomes a problem.
If the document gets changed and doesnot fall in the selector criteria anymore then the change function is not called.
The behaviour you described is correct.
The filtered changes feed only includes the documents that currently match the selector conditions. The filtering is not considering the previous state of the document.
This question exposes a similar issue: CouchDB filter function and continuous feed
I see 2 approaches to manage this:
Make filtering data part of the document identity:
Design your docs by including the filtering attributes as part of the document IDs, so the change of any of the filtering attributes should be managed as a deletion and a creation of a new document. The local database will contain only the valid documents.
Keep history of the filtering data in the doc:
You should keep the history of the values of the filtering attributes in the documents and expand your selector to check the current and old values. In this case, is your client application the responsible to determine which of the documents in the local database are valid in a moment as not every document in the local database is valid for the client.

How to best reach into MongoDb Mongoose Schema objects which use lists of other objects

I am learning my way around MongoDB data types and the best way to use documents and Schemas through Mongoose.
I've defined a couple of Schemas for a Navigation bar object which stores the navigation items as a list, and each item is defined by a schema with the properties name, type, url, and a list of drop downs if it has any (if the type is "dropdown").
Here are those Schemas:
var navSchema = new Schema({
id: String,
items: [Schema.ObjectId]
});
is the nav object, and
var navItemSchema = new Schema({
name: String,
type: {type: String, default: "link"},
url: {type: String, default: null},
dropdowns: {type: [Schema.ObjectId], default: null}
});
is the nav item schema, but each nav item can potentially have dropdowns, and so the dropdowns is a list of nav items, which can also potentially have dropdowns. But in this case, only a few do.
Now to create the data for these objects, I have to do something like this to create a nav item, example for "home"
var home = new navItem({
name: "home",
url: "/home"
});
but for items with dropdowns, I have to define all the items I know will be dropdowns before defining list which includes those items, and then defining the parent item and using the list with those items I just defined. Like this
var allAccessories = new navItem({
name: "all accessories",
url: "/accessories"
});
var cushions = new navItem({
name: "cushions",
url: "/accessories#cushions"
});
var cupHolders = new navItem({
name: "cup holders",
url: "/accessories#cupholders"
});
var accessoriesDropdownItems = [
allAccessories,
cushions,
cupHolders
];
var accessories = new navItem({
name: "accessories",
type: "dropdown",
dropdowns: accessoriesDropdownItems
});
So, I assume I'm doing that right..? My only gripe with this method is that in my nav.js file where I create this mongodb object/collection, I have to think about what items are going to be used in dropdowns of other items, and so I have to theoretically order them to be defined before the other variables are defined in the document.
But if I then want to use an item in two dropdown lists, and one of those dropdownlists I happened to have already defined above it in the document but now want to add to. I have to move the item definition above anywhere it's used, this ruins the organisation of the document..
But I'm okay with physically indenting to keep my work organised and sorted.
My question is how do I actually access properties of objects within lists of other objects.
Straight up I define Nav as simply an object with an id, "nav" because I don't want to use its _id ObjectId to reference it all the time...? And an items array which contains the navItemsSchema objects.
But when I try to reach into these objects through mongo shell using something like
db.navs.find({items: {$elemMatch: {name:"home"}}})
Or
db.navs.find({items: ObjectId("58d5d6df0789f718460ff278")})
Or
db.navs.find({items:{ "name" : "home"}})
I can't get any data back.. I have successfully manage to return all objects in the collection through the node app via responding found nav in navs
app.get('/nav', function(req, res) {
mongoose.model('nav').find(function(err, nav) {
res.send(nav);
});
});
But all this returns me is a data structure with object id's and not the actually data of the objects.
{"_id":"58d5d6df0789f718460ff287",
"id":"nav","__v":0,
"items":
["58d5d6df0789f718460ff278",
"58d5d6df0789f718460ff279",
"58d5d6df0789f718460ff286",
"58d5d6df0789f718460ff281",
"58d5d6df0789f718460ff282",
"58d5d6df0789f718460ff283",
"58d5d6df0789f718460ff284"
]
}
Could you please help me understand how I reach into these data hierarchies via say db.navs.nav("nav").items.findAll() and it lists all the items and their json?
Or is this not possible and you need to loop through all items, then. Wait, where are the objects stored corresponding to ObjectId's in the items list?
So I actually figured it out myself. I was using type: [Schema.ObjectId] (ie. type was a list of Schema.ObjectIds) but I didn't know that that automatically parses the inputted list of objects and extracts just their ObjectId's and stores them, all I need to do was make the type: [] (ie. just a list) and then I can traverse my nav object simply by... Oh wait, for some reason when mongoose nav.saves my nav it becomes an object inside a list. So when I mongoose.model('nav').find(function(err, nav) {} it gives me a nav object which I need to use via nav[0].items[0].name Or I can go nav = nav[0] but I wish I didn't need to do this step? Maybe there's an answer but yes. Otherwise. This is the solution I was looking for.

sailsjs - one POST multiple records and associations created

Invoice-Model:
attributes: {
number: {
type: 'integer'
}
lines: {
collection: 'line',
via: 'invoice'
}
}
Line-Model:
attributes: {
name: {
type: 'integer'
}
invoice: {
model: 'invoice'
}
}
As you can see these models have a One-To-Many relationship. Everything is working fine.
But now I want to create a new Invoice and new Lines with the Blueprint API that are associated.
The documentation says you can create a new record and add it to an existing one with this schema: POST /:model/:id/:association/:fk
But it does not state if it is possible to create two records at the same time and associate them.
More details: I've got an invoice and in this invoice you can add lines with products, their quantity and other stuff. Now when the user clicks on save, I need to create a new invoice and the new lines and associate them somehow.
Should I create a custom Controller Action for this, or am I overthinking this and I should do this whole thing completely different?
To create a new invoice with new lines is possible to do in SailsJS with the following code:
Invoice.create({number: 1, lines: [
{
name: '1'
}
]})
This will create a new invoice with number 1 and also creates a new line with the name 1. Line 1 will be related with invoice.
Since lines is an collection you can add them as a array, so this makes it possible to add more than one Line to your Invoice.
You can overwrite your create function in the InvoiceController and add this code.
An alternative solution is using Promises. Make sure you install bluebird by using the command:
npm install bluebird
Put the following code in the top of your controller
var Promise = require('bluebird');
You can use the following code:
createWithPromises: function(req, res){
var lineName = 1;
var invoiceNumber = 2;
Invoice.create({number: invoiceNumber})
.then(function(result){
Line.create({name: lineName, invoice: result})
})
.then(function(result){
sails.log(result);
})
}
First it creates an Invoice with number 2. If succeed it will create a Line, and as parameter for invoice, you give the result from your previous create call.
For information about Promises check http://bluebirdjs.com/docs/getting-started.html

SailsJs/Express removing nested array `res.send()`

I'm using SailsJs (which is Express based) to send an JSON object with an array. For some reason, when I load the API in my browser, the array is not sent.
The code that sends the object is here:
exports.RESTifySend = function(res, objects) {
return RESTService.RESTify(objects).then(function(RESTedObjects) {
console.log("SENDING: ", RESTedObjects);
return res.json(RESTedObjects, 200); // I've also tried res.send()
}, function() {
res.send(500);
});
};
The logging statement SENDING: ... outputs:
SENDING: {
id: 'IKIlrgXhp6',
messages: [{
user: null,
text: 'trest',
sentAt: undefined
}]
}
The RESTifyService is just a small framework I built to remove object attributes that shouldn't be exposed in the API (passwords, emails, etc.).
Somewhere in the framework I built, I replaced toObject(); with lodash.clone([object]), which solved all the problems. For others experiencing a similar issue, I suggest trying the same thing in your toJSON method. The toObject(); method of waterline objects apparently has some odd side effects when you true to populate or edit an attribute that matches the name of an association.
Also ran into this issue when serializing a model with a collection attribute (like something with many Comments).
In my case I call .toJSON on the record object with the collection during serialization, which has a model definition like:
attributes: {
...
toJSON: function() {
var self = this.toObject()
// could pick a subset of attrs here
return _.pick(self, _.keys(self))
}
}
Which leaves me free to then populate the comments attribute and not have surprises about the data on the wire.

Resources