Get values from localStorage to use in Sencha Touch AJAX Proxy - sencha-touch

In my Sencha Touch application I store some values in localStorage and they get stored just fine. Later I need to use those values for a request to a store and I need to pass those values as request headers. The code looks like the following:
Ext.define('AppName.store.storeName', {
extend: 'Ext.data.Store',
requires: [
'AppName.model.modelName'
],
config: {
model: 'AppName.model.modelName',
storeId: 'storeName',
proxy: {
type: 'ajax',
url: 'http://dynamic',
headers: {
auth_token: localStorage.getItem('activeUserToken'),
user_email: localStorage.getItem('userEMail')
},
reader: {
type: 'json'
}
}
}
});
However, both auth_token and user_email values are being returned as "undefined" when in Chrome developer tools I clearly see those values. Besides of that I access those values in many other places in my Sencha Touch application but can't do that in the store itself.
Could anybody please tell me what I'm doing wrong?
Thanks

I've solved this problem in the following way. Instead of passing the headers in the store I'm passing the headers during the request, before loading the store. Here's the code for that:
Ext.getStore('storeName').getProxy().setUrl(someURL);
Ext.getStore('storeName').getProxy().setHeaders(
{
"auth_token" : activeUserToken,
"user_email" : userEmail
}
);
Ext.getStore('storeName').load();

Related

When providing pregenerated links for supporting multiple API specs Swagger UI doesn't load the specs

I have a service written in Go that also uses go templates for frontend. This service is used by our external third parties as a portal for looking up stuff and searching. There is another service that is a rest API for handling orders. The portal service has a page where you can look up API docs.
I had only one API version and I use SwaggerUI for showing API docs. I had to create a new endpoint and make it a part of a new API version. Now I want to show a new API version but also the old one for supporting old clients. Something like this:
So when a user clicks a button on the portal website to see documentation, the request is handled by this function in portal (Note: I already refactored this function to support multiple urls):
func getDocs(c echo.Context) error {
source := c.Get(auth.SourceName).(string)
key := c.Get(auth.KeyName).(string)
jsonURLs := []DocsURL{
{
url: fmt.Sprintf("%s/0.1/docs?source=%s", config.baseURL, key),
name: "0.1"
},
{
url: fmt.Sprintf("%s/1.0/docs?source=%s", config.baseURL, key),
name: "0.1"
},
}
return c.Render(http.StatusOK, "docs/index", map[string]interface{}{
"source": source,
"key": key,
"pageType": "docs",
"jsonURLs": jsonURLs,
})
}
And this is the script from the template where I use SwaggerUI:
window.onload = function() {
const ui = SwaggerUIBundle({
urls: {{ .jsonURLs }},
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
supportedSubmitMethods: []
})
window.ui = ui
$('.tryitout').prop('disabled', true);
}
I need to generate links depending on the environment (production, staging, local). Also it makes sense to generate data on the backend and feed it to the template to display. BUT THIS DOENS'T WORK!
However, if I hard-code the links in the SwaggerUIBundle it works and portal shows a correct drop-down menu and allows to switch between versions and loads docs for the corresponding version.
func getDocs(c echo.Context) error {
source := c.Get(auth.SourceName).(string)
key := c.Get(auth.KeyName).(string)
return c.Render(http.StatusOK, "docs/index", map[string]interface{}{
"source": source,
"key": key,
"pageType": "docs",
})
}
In the template:
window.onload = function() {
const ui = SwaggerUIBundle({
urls: [
{
url: "http://localhost:8088/0.1/docs?source=111111",
name: "0.1"
},
{
url: "http://localhost:8088/1.0/docs?source=111111",
name: "1.0"
}
],
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
supportedSubmitMethods: []
})
window.ui = ui
$('.tryitout').prop('disabled', true);
}
Why is that? And is there a way to make the first version of code work?
I need the links to be dynamic and preferably generated in the handler. Thank you!
There appear to be two mistakes:
First:
urls: {{ .jsonURLs }},
This will not write jsonURLs in JSON format for you. It will simply write a string representation of jsonURLs. Either you need to write the template to iterate elements of jsonURLs and print them out one by one, or marshal jsonURLs to json yourself:
jsonText,_:=json.Marshal(jsonURLs)
return c.Render(http.StatusOK, "docs/index", map[string]interface{}{
"source": source,
"key": key,
"pageType": "docs",
"jsonURLs": string(jsonText),
})
Second: it looks like you didn't export the member fields of DocsURL struct. Capitalize the field names and add json tags.
type DocsURL struct {
URL string `json:"url"`
Name string `json:"name"`
}

Agile Central Basic App Settings

I'm implementing a simple app based on the UserIterationCapacity using a Rally.app.TimeboxScopedApp.
Now I want to specify a couple of settings as App settings and found the developer tutorial for this: https://help.rallydev.com/apps/2.1/doc/#!/guide/settings
But I just cant get it working. Whenever I try to fetch a setting my code stops without any warnings.
I've implemented the following:
config: {
defaultSettings: {
hoursPerSp: 6,
decimalsOnHoursPerSp: 1
}
},
getSettingsFields: function() {
return [
{
name: 'hoursPerSp',
xtype: 'rallynumberfield'
},
{
name: 'decimalsOnHoursPerSp',
xtype: 'rallynumberfield'
}
];
},
Now I'm trying to use
this.getSettings('hoursPerSp');
but unfortunately it is not working.
Thank you in advance
Problem solved.
I needed to keep track of my scope, i.e., I needed to pass in the this variable to my renders.

Data Testing in EmberJS and QUnit using fixtures

I have a simple ember application and I want to test it, without making server for API calls.
I've looked around a bunch and found this piece of code which helps tremendously. Problem is for testing I want to use the fixture adapter (makes sense, right?)
#store = Lfg.__container__.lookup('store:main')
Here is my model:
Lfg.Activity = DS.Model.extend
title: DS.attr('string')
people: DS.attr('number')
maxPeople: DS.attr('number')
host: DS.attr('string')
Then inside an Em.run => I do this
Lfg.reset()
container = new Ember.Container()
# These are my models... just one for now.
[
'activity'
].forEach (x,i) ->
container.register 'model:'+x, Lfg.get( Ember.String.classify(x) )
# It's essentially just: container.register("model:activity", Lfg.Activity)
#store = DS.Store.create
adapter: DS.FixtureAdapter.extend()
container: container
But I keep getting errors with the serializer. I tried adding the serializer but doesn't help. Do I need to container.register other things as well?
The error I get is TypeError: Cannot call method 'serialize' of undefined coming from mockJSON method, more specifically store.serializerFor(type) is returning null.
If I set store via store = Lfg.__container__.lookup('store:main') and then store.serializerFor(Lfg.Activity) it seems to work ok in the console -- is this not the same store? I want to use the one with the fixture adapter. I tried setting the serializer but that didn't help.
I prefer using something like mockjax to mock the api endpoints, then using qunit and the built in helpers provided by Ember and qunit
Here's an example of how to set up a simple json response
$.mockjax({
url: '/colors',
dataType: 'json',
responseText: {
colors:[
{
id: 1,
color: "red"
},
{
id: 2,
color: "green"
},
{
id: 3,
color: "blue"
}
]
}
});
And a test that would hit this endpoint
test("root lists 3 colors", function(){
var store = App.__container__.lookup('store:main');
var colors = store.find('color');
stop();
colors.then(function(arr){
start();
equal(arr.get('length'), 3, 'store returns 3 records');
});
});
http://emberjs.jsbin.com/wipo/3/edit

Extjs 4.1, Making JSON array for Store sync

When I sync for edited grid, extjs pass JSON data to Server by AJAX.
If I edit multiple row, and sync then it make JSON array. But if I edit just single row,
it will pass just a JSON data.
So I have some problem with receiving the parameter because the parameter type is vary.
My question is,
Is it possible to make JSON array date even for single edited grid?
If so, how should I do? anybody know please please advice me.
[Single]
[Multiple]
And this is part of grid store,
proxy: {
type: "ajax",
api: {
update: 'Order/ItemUpdate',
read: 'Order/ItemList',
create: undefined,
destroy: undefined
}
}
Just set the allowSingle config of Ext.data.writer.Json to false. As covered here in the docs.
I am pretty sure that this can be done from your proxy config, e.g.:
proxy: {
type: 'ajax',
writer: {
type: 'json',
allowSingle: false
},
api: {
update: 'Order/ItemUpdate',
read: 'Order/ItemList',
create: undefined,
destroy: undefined
}
}

Sencha touch 2.0 : Store loading, first availability

I'm trying to figure out when a store is ready to be used within my app.
I figured from the doc that if I want to display information from my store, I should listen to the 'refresh' event of my store to get notified when it has been changed (and thus also when it is first loaded).
However, using the following example:
Ext.define('MyApp.store.Config', {
extend: 'Ext.data.Store',
config: {
autoLoad: true,
autoSync: true,
model: 'MyApp.model.Config',
listeners: {
refresh: function() {
console.log(Ext.StoreManager.get('Config').getAt(0))
}
}
} });
the 'console.log' gets called twice at startup, and it fails the first time (it seems that the Store is not loaded yet). My model uses a proxy (type ajax, and a json reader).
Could someone tell me how I should proceed to avoid this error?
Thanks!
I found the reason...
I was declaring the 'stores:['Config']' property both in my app.js and in on of my controllers.
Quite hard to spot but my mistake...