How to update content in a block after flagging event without page reload? - drupal-8

Hy, I'm fairly new to Drupal 8. I have a custom entity type called Event and using the Flag module I added the option to "Favorite the event" and finally I have a custom block which displays the number of Events each User has favorited. Everytime a user "favorites" a new event the block obviously shows the old value. I have set up cache invalidation, so if the user refreshes the page he will see the updated/correct number. Now my question is, how can I update the block's number without refreshing the page. I can't find an easy example anywhere. The Flag module's docs are outdated or incomplete and I think the answer lies in subscribing to the Flagging event and performing actions there, I just don't know how to include or perform a javascript action ("if it's needed, I assume it is). Any help would be greatly appreciated.

Use Ajax Command on block and don't forget to attach ajax.core library to block.

Related

Document has been modified or corrupted since signed! (data)

I'm working in HCL Notes application. I have developed a summary view to show calculated figures to the user. Then the user clicks one of the action buttons and I open a detailed view, but for that view I setup Selection Formula on the fly so that it shows the records filtered specific to that button conditions. It was working almost fine for a few days, but now most of the time it shows some previously shown (filtered) data no matter what button the user has clicked. Means it doesn't set the Selection Formula of the view and shows the view with the old formula and it won't get back to normal condition even if they restart Notes application.
When the user is stuck in this particular condition, and they peep through the status bar it shows this message:
Document has been modified or corrupted since signed! (data).
The necessary code-snippet is as below:
*Set dtlView = db.GetView("Report_Dtl")
dtlView.SelectionFormula =formula
Call dtlView.Refresh()*
where formula is the dynamically built formula. Looks like the line
dtlView.SelectionFormula =formula
is unable to update the selection formula and then the line below generates the above error message:
Call uidb.OpenView(dtlView.Name,,False, False)
Please help!
Thanks
For "on the fly" modification of the view selection formula your user need "Designer"- access to the database, and that is never a good idea. The help document to the function you are using is explicitly stating that this is not a good idea (emphasise of mine):
This is not a good way to display a selected set of documents for a specific user. When you use this property to change the view selection, it applies to all users of the view.
There are problems with using this method to make a view display a new selection of documents for an end user:
Do not give end-users Designer access to an application.
If it is a shared view, users will interfere with each other's searches.
The NotesĀ® client caches design information, and there's no way to tell it to update its cache (except for outlines). Exiting and re-entering the application usually works, but it's hard to programmatically ensure the user exited the application entirely.
In addition the modification of the view selection formula can break the signature of the design element and then other errors occur.
Better use another approach:
Use a Folder for every user and put the selected documents in there (after doing a NotesDatabase.Search with the formula
Use a separate view for every user and let a server agent manipulate its selection formula with a user that has access.
For having a separate view / folder for every user you could use "Shared, Private on first use"- views (they are not easy to maintain), or any process that generates them and is able to assign every view to the users they belong to... in both cases this needs some effort, but at least it will work.

How to detect a period of inactivity in EmberJS?

I am trying to detect a period of user inactivity in my EmberJS application so that, after a certain duration I can call a session.inValidate method and revoke their authentication.
The only resource I have been able to find is an article here which I was advised in an original question is out of date and doesn't use current Ember concepts.
The out-of-date article suggested adding a property to the App object and updating the property on certain events like mousemove but I was unable to find a location to set this property correctly in either app.js or the config environment.
ember-concurrency was recommended but after looking at some of the documentation I still don't understand where such code fits in an EmberJS application structure.
Unfortunately I'm stuck at a point before I can give a code sample. Any advice is appreciated.
Edit: As per #hernanvicente excellent question, I am defining inactivity as 'no user input'. So no mousemove or key press events within X period
The hard question here is what activity actually means. Generally I would advise against your idea. There is no way to know if the user is actually still looking on your page.
However if you really want to do something after the user hasn't done something for some amount of time the interesting question is where to place your code.
You have multiple options here, but probably not the worst option would be to use the application controller. You could also use a service, but then you need to use that service somewhere to initialize it. You could also just use a component.
If you want to couple this to authentication you could consider to use the existing session service.
However everything else is not really ember specific. You can just place some code in the application controllers init hook and access document.addEventListener(). Then you could just save a timestamp away and act accordingly.
To have a bit a nicer API for waiting you can utilize ember-concurrency with a restartable task:
didSomething: task(function * () {
yield timeout(1000); // number of miliseconds to wait
logout();
}).restartable(),
here logout() will be called, after the task hasn't been called for 1 second. A nice visual demonstration about task concurrency and restarable can be found here.
Here is an ember-twiddle showing a complete example.

In sitecore 8,I get error as "The Controls Collection can't be modified because the control contains Code blocks i.e( <%...%>)"

In sitecore 8, After publishing my VS solution I am able to see few pages while for few pages I get error as "THe Controls Collection can't be modified because the control contains Code blocks i.e( <%...%>)". Please help.
This is typically caused by using code blocks in a Sitecore layout. This will work fine in the Content Editor however as soon as you go to use the Page Editor or Preview a page you will run into this error. This happens because Sitecore dynamically adds the ribbon control to the page which modifies the controls collection.
However because you've mentioned you are seeing it on a published page this may actually not be related to Sitecore and it's ribbon control at all (this can happen in vanilla Asp.net as well). It could just be happening because you are using code blocks to inject values into the header of the page and the controls you are referencing have not been rendered yet. Based on the error message included it looks like you (or at least the Active Commerce skin) may be trying build the meta tags in this fashion.
There are a few options here for solving this issue:
If trying to inject something into your JavaScript (e.g. the ClientID of a field) then do so using Page.ClientScript.RegisterClientScriptBlock.
If you are trying to inject a value into JavaScript you can also move the JavaScript block to the end of the page (before the tag) or in a script block after the control it is referencing. Having it in the will often result in this error.
Use a data binding block instead (<%# %>). If you are accessing a page property you will need to add Page.DataBind() in the Page_Load.
If those don't get you up and running share any relevant code and I should be able to help you further.
References:
http://www.tcsc.com/sitecore-layouts-and-code-blocks/ https://weblogs.asp.net/abdullaabdelhaq/how-to-fix-this-the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks-i-e-lt-gt

How can I remove duplicated page events in Sitecore 8.1?

I have a processor in the <registerPageEvent> pipeline that checks whether an event is duplicated and removes it. It works well on 7.2, but I have to upgrade to 8.1 Update 3 and I can't find a way to remove an already added page event.
Is this possible and how can I do that?
Piece of code from 7.2:
Tracker.Visitor.DataSet.PageEvents.Rows.RemoveAt(Tracker.Visitor.DataSet.PageEvents.Count - 1);
You cannot remove events via the API starting from Sitecore 7.5. (The only way around would be to use .NET reflection)
Even in 7.2, doing this was a bad idea. The reason being, when an event is added via the API, some internal logic is applied - e.g. the visit's total value is recalculated. If you remove an event manually, these internal actions will not be triggered, and hence, your visit data may end up in an inconsistent state.
The best approach for you would be to add checks in the code that registers your events - an event should only be registered if it's not a duplicate of an existing event.

Sitecore item will not go into edit mode; any suggestions on how to troubleshoot?

I have several Sitecore items (whose template is connected to an approval workflow) that will not go into Edit mode when clicking "Edit" from the "Review" tab. The same workflow is used elsewhere on the site successfully (the site is more than 3 years old), but clicking Edit in this case doesn't seem to affect the workflow state.
I've turned on verbose logging, which provides a lot of detail, but nothing to indicate any error conditions. I've been comparing how this template is configured with other "working" templates for a couple of days and everything looks Kosher, but surely I'm missing something obvious.
EDIT: The item(s) in question are imported by a data provider, and I've confirmed that for some reason Sitecore is not setting the "Workflow" and "State" system standard fields with a default value. That appears to be at least part of the issue.
EDIT 2: Followed the steps in this thread http://sdn.sitecore.net/Forum/ShowPost.aspx?PostID=45991 to use Sitecore Rocks to update all of the items to the correct Workflow State, but the update did not stick. For some reason workflow changes are not persisted for imported items.
EDIT 3: Clicking the Edit dropdown button under the Home tab displays the message "The item is currently not part of a workflow."
Standard values set correctly? Only have one Standard Values item for this template?
Generally, almost every time I run into a situation where X works on certain items, but not on others... it's a permissions issue. You've tested this as an Administrator I hope?
Sorry my suggestions are so vague, but it's hard to be specific here. We need some more information from you... When you click on Edit what exactly are you expecting to happen? I'm not entirely sure what you mean by "Edit mode" here. In Content Editor, all items are essentially in edit mode all the time... In my configuration, when I click on Edit, I am just locking the item. It does not change workflow state or create a new version.
These items are coming from a data provider... is your data provider handling Lock/Unlock calls properly? I know it's possible to use a data provider without implementing the entire Item API.